FirstCommit
second commit 3rd commit - made basic skeleton 3rd commit - made basic skeleton new update for thread RealService test for. Correct using bytearray. Correct using bytearray. Correct using bytearray.
This commit is contained in:
		
							parent
							
								
									fd9c7bf382
								
							
						
					
					
						commit
						f111bd45b4
					
				| @ -2,6 +2,11 @@ | |||||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|     package="com.flywithu.carnav" |     package="com.flywithu.carnav" | ||||||
|     xmlns:tools="http://schemas.android.com/tools"> |     xmlns:tools="http://schemas.android.com/tools"> | ||||||
|  |     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | ||||||
|  |     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> | ||||||
|  |     <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> | ||||||
|  | 
 | ||||||
|  |     <uses-feature android:name="android.hardware.usb.host" /> | ||||||
| 
 | 
 | ||||||
|     <application |     <application | ||||||
|         android:allowBackup="true" |         android:allowBackup="true" | ||||||
| @ -27,12 +32,24 @@ | |||||||
|                 android:resource="@xml/device_filter" /> |                 android:resource="@xml/device_filter" /> | ||||||
| 
 | 
 | ||||||
|         </service> |         </service> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         <service | ||||||
|  |             android:name=".RestartService" | ||||||
|  |             android:enabled="true" | ||||||
|  |             android:exported="true" /> | ||||||
|  |         <receiver android:name=".AlarmReceiver"/> | ||||||
|  |         <receiver android:name=".RebootReceiver"> | ||||||
|  |             <intent-filter> | ||||||
|  |                 <action android:name="android.intent.action.BOOT_COMPLETED"/> | ||||||
|  |             </intent-filter> | ||||||
|  |         </receiver> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|     </application> |     </application> | ||||||
| 
 | 
 | ||||||
|     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> |  | ||||||
|     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> |  | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> |  | ||||||
|     <uses-sdk tools:overrideLibrary="com.hoho.android.usbserial"/> |     <uses-sdk tools:overrideLibrary="com.hoho.android.usbserial"/> | ||||||
| </manifest> | </manifest> | ||||||
| @ -5,7 +5,7 @@ import android.content.Context; | |||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
| import android.os.Build; | import android.os.Build; | ||||||
| 
 | 
 | ||||||
| public class AlarmRecever extends BroadcastReceiver{ | public class AlarmReceiver extends BroadcastReceiver{ | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void onReceive(Context context, Intent intent) { |     public void onReceive(Context context, Intent intent) { | ||||||
| @ -1,35 +1,121 @@ | |||||||
| package com.flywithu.carnav; | package com.flywithu.carnav; | ||||||
| 
 | 
 | ||||||
| import androidx.appcompat.app.AppCompatActivity; | import android.app.PendingIntent; | ||||||
| 
 |  | ||||||
| import android.content.BroadcastReceiver; | import android.content.BroadcastReceiver; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
| import android.content.IntentFilter; | import android.content.IntentFilter; | ||||||
|  | import android.hardware.usb.UsbDeviceConnection; | ||||||
|  | import android.hardware.usb.UsbManager; | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.util.Log; |  | ||||||
| import android.widget.Toast; | import android.widget.Toast; | ||||||
| 
 | 
 | ||||||
|  | import androidx.appcompat.app.AppCompatActivity; | ||||||
|  | 
 | ||||||
|  | import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||||||
|  | import com.hoho.android.usbserial.driver.UsbSerialPort; | ||||||
|  | import com.hoho.android.usbserial.driver.UsbSerialProber; | ||||||
|  | 
 | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
| public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||||
| 
 | 
 | ||||||
|     private Intent serviceIntent; |  | ||||||
|     public static final String SERVICECMD = "com.android.music.musicservicecommand"; |     public static final String SERVICECMD = "com.android.music.musicservicecommand"; | ||||||
|     public static final String TAG = "carnav"; |     public static final String TAG = "carnav"; | ||||||
|     @Override |     static final String ACTION_USB_PERMISSION = "com.flywithu.carnav.USB_PERMISSION"; | ||||||
|     protected void onCreate(Bundle savedInstanceState) { |     private Intent serviceIntent; | ||||||
|         super.onCreate(savedInstanceState); |     private BroadcastReceiver usbReceiver = null; | ||||||
|         setContentView(R.layout.activity_main); | 
 | ||||||
|  |     private void goService() { | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|         if (RealService.serviceIntent == null) { |         if (RealService.serviceIntent == null) { | ||||||
|  | 
 | ||||||
|             serviceIntent = new Intent(this, RealService.class); |             serviceIntent = new Intent(this, RealService.class); | ||||||
|             startService(serviceIntent); |             startService(serviceIntent); | ||||||
|  |             Toast.makeText(getApplicationContext(), "new start", Toast.LENGTH_LONG).show(); | ||||||
|         } else { |         } else { | ||||||
|             serviceIntent = RealService.serviceIntent;//getInstance().getApplication(); |             serviceIntent = RealService.serviceIntent;//getInstance().getApplication(); | ||||||
|             Toast.makeText(getApplicationContext(), "already", Toast.LENGTH_LONG).show(); |             Toast.makeText(getApplicationContext(), "already", Toast.LENGTH_LONG).show(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     protected void onCreate(Bundle savedInstanceState) { | ||||||
|  |         super.onCreate(savedInstanceState); | ||||||
|  |         setContentView(R.layout.activity_main); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); | ||||||
|  |         List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager); | ||||||
|  |         if (!availableDrivers.isEmpty()) { | ||||||
|  |             Toast.makeText(getApplicationContext(), "USB Exist", Toast.LENGTH_LONG).show(); | ||||||
|  | 
 | ||||||
|  |             UsbSerialDriver driver = null; | ||||||
|  |             UsbDeviceConnection connection = null; | ||||||
|  |             try { | ||||||
|  |                 driver = availableDrivers.get(0); | ||||||
|  |                 if (!manager.hasPermission(driver.getDevice())) { | ||||||
|  | 
 | ||||||
|  |                     Toast.makeText(getApplicationContext(), "NO Permission", Toast.LENGTH_LONG).show(); | ||||||
|  |                     //Request USB HOST | ||||||
|  |                     // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..) | ||||||
|  |                     if (!manager.hasPermission(driver.getDevice())) { | ||||||
|  | 
 | ||||||
|  |                         PendingIntent mPermissionIntent; | ||||||
|  |                         mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this, 0, | ||||||
|  |                                 new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT); | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  |                         usbReceiver = new BroadcastReceiver() { | ||||||
|  |                             @Override | ||||||
|  |                             public void onReceive(Context context, Intent intent) { | ||||||
|  |                                 String action = intent.getAction(); | ||||||
|  |                                 if (ACTION_USB_PERMISSION.equals(action)) { | ||||||
|  | 
 | ||||||
|  |                                     if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true)) { | ||||||
|  |                                         Toast.makeText(getApplicationContext(), "Pemission Granted", Toast.LENGTH_LONG).show(); | ||||||
|  |                                         goService(); | ||||||
|  |                                     } else { | ||||||
|  |                                         Toast.makeText(getApplicationContext(), "Pemission Denied", Toast.LENGTH_LONG).show(); | ||||||
|  |                                     } | ||||||
|  |                                 } | ||||||
| 
 | 
 | ||||||
|                             } |                             } | ||||||
|  |                         }; | ||||||
|  | 
 | ||||||
|  |                         registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION)); | ||||||
|  |                         manager.requestPermission(driver.getDevice(), mPermissionIntent); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     Toast.makeText(getApplicationContext(), "Has Permission", Toast.LENGTH_LONG).show(); | ||||||
|  |                     connection = manager.openDevice(driver.getDevice()); | ||||||
|  |                     if (connection == null) { | ||||||
|  |                         Toast.makeText(getApplicationContext(), "Unknown error", Toast.LENGTH_LONG).show(); | ||||||
|  |                     } else { | ||||||
|  |                         UsbSerialPort sPort = null; | ||||||
|  |                         sPort = driver.getPorts().get(0); | ||||||
|  |                         sPort.open(connection); | ||||||
|  |                         Toast.makeText(getApplicationContext(), "Serial Count:" + String.valueOf(driver.getPorts().size()), Toast.LENGTH_LONG).show(); | ||||||
|  | 
 | ||||||
|  |                         sPort.close(); | ||||||
|  |                         sPort = null; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |                         goService(); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } catch (Exception e) { | ||||||
|  |                 Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); | ||||||
|  |             } | ||||||
|  |         } else { | ||||||
|  |             Toast.makeText(getApplicationContext(), "NO USB", Toast.LENGTH_LONG).show(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     protected void onDestroy() { |     protected void onDestroy() { | ||||||
| @ -38,6 +124,9 @@ public class MainActivity extends AppCompatActivity { | |||||||
|             stopService(serviceIntent); |             stopService(serviceIntent); | ||||||
|             serviceIntent = null; |             serviceIntent = null; | ||||||
|         } |         } | ||||||
|  |         if (usbReceiver != null) { | ||||||
|  |             unregisterReceiver(usbReceiver); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										74
									
								
								app/src/main/java/com/flywithu/carnav/MusicTitle.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								app/src/main/java/com/flywithu/carnav/MusicTitle.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,74 @@ | |||||||
|  | package com.flywithu.carnav; | ||||||
|  | 
 | ||||||
|  | public class MusicTitle { | ||||||
|  | 
 | ||||||
|  |     private String artist = null; | ||||||
|  |     private String album = null; | ||||||
|  |     private String track = null; | ||||||
|  |     private boolean isRunning = false; | ||||||
|  |     private int total = 0; | ||||||
|  |     private int position = 0; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     public String getAlbum() { | ||||||
|  |         if(album == null) | ||||||
|  |         { | ||||||
|  |             album = "getAlbum"; | ||||||
|  |         } | ||||||
|  |         return album; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setAlbum(String album) { | ||||||
|  |         this.album = album; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getTrack() { | ||||||
|  |         if(track == null) | ||||||
|  |         { | ||||||
|  |             track = "getTrack"; | ||||||
|  |         } | ||||||
|  |         return track; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setTrack(String track) { | ||||||
|  |         this.track = track; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isRunning() { | ||||||
|  |         return isRunning; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setRunning(boolean running) { | ||||||
|  |         isRunning = running; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int getTotal() { | ||||||
|  |         return 100; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     //public void setTotal(int total) { | ||||||
|  |    //     this.total = total; | ||||||
|  |     //} | ||||||
|  | 
 | ||||||
|  |     public int getPosition() { | ||||||
|  |         return position; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setPosition(int position) { | ||||||
|  |         this.position = position; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     public String getArtist() { | ||||||
|  |         if(artist == null) | ||||||
|  |         { | ||||||
|  |             artist = "getArtist"; | ||||||
|  |         } | ||||||
|  |         return artist; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setArtist(String artist) { | ||||||
|  |         this.artist = artist; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -2,9 +2,6 @@ package com.flywithu.carnav; | |||||||
| 
 | 
 | ||||||
| import android.app.AlarmManager; | import android.app.AlarmManager; | ||||||
| import android.app.Application; | import android.app.Application; | ||||||
| import android.app.Notification; |  | ||||||
| import android.app.NotificationChannel; |  | ||||||
| import android.app.NotificationManager; |  | ||||||
| import android.app.PendingIntent; | import android.app.PendingIntent; | ||||||
| import android.app.Service; | import android.app.Service; | ||||||
| import android.content.BroadcastReceiver; | import android.content.BroadcastReceiver; | ||||||
| @ -13,22 +10,19 @@ import android.content.Intent; | |||||||
| import android.content.IntentFilter; | import android.content.IntentFilter; | ||||||
| import android.hardware.usb.UsbDeviceConnection; | import android.hardware.usb.UsbDeviceConnection; | ||||||
| import android.hardware.usb.UsbManager; | import android.hardware.usb.UsbManager; | ||||||
| import android.media.RingtoneManager; | import android.os.Bundle; | ||||||
| import android.net.Uri; |  | ||||||
| import android.os.Build; |  | ||||||
| import android.os.Handler; | import android.os.Handler; | ||||||
| import android.os.IBinder; | import android.os.IBinder; | ||||||
| 
 |  | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| import android.widget.Toast; | import android.widget.Toast; | ||||||
| 
 | 
 | ||||||
| import androidx.core.app.NotificationCompat; |  | ||||||
| 
 |  | ||||||
| import com.hoho.android.usbserial.driver.UsbSerialDriver; | import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||||||
| import com.hoho.android.usbserial.driver.UsbSerialPort; | import com.hoho.android.usbserial.driver.UsbSerialPort; | ||||||
| import com.hoho.android.usbserial.driver.UsbSerialProber; | import com.hoho.android.usbserial.driver.UsbSerialProber; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.nio.ByteBuffer; | ||||||
|  | import java.nio.charset.Charset; | ||||||
| import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||||
| import java.util.Calendar; | import java.util.Calendar; | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
| @ -39,25 +33,100 @@ public class RealService extends Service { | |||||||
|     public static Intent serviceIntent = null; |     public static Intent serviceIntent = null; | ||||||
| 
 | 
 | ||||||
|     private static UsbSerialPort sPort = null; |     private static UsbSerialPort sPort = null; | ||||||
|  |     private MusicTitle currentSong = new MusicTitle(); | ||||||
|  |     private BroadcastReceiver mReceiver = new BroadcastReceiver() { | ||||||
| 
 | 
 | ||||||
|  |         @Override | ||||||
|  |         public void onReceive(Context context, Intent intent) { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |             Bundle bundle = intent.getExtras(); | ||||||
|  |             if (bundle != null) { | ||||||
|  |                 for (String key : bundle.keySet()) { | ||||||
|  |                     Object value = bundle.get(key); | ||||||
|  |                     Log.d("MUSIC", String.format("%s %s (%s)", key, | ||||||
|  |                             value.toString(), value.getClass().getName())); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             String action = intent.getAction(); | ||||||
|  |             String cmd = intent.getStringExtra("command"); | ||||||
|  |             Log.v("MUSIC ", action + " / " + cmd); | ||||||
|  |             String artist = intent.getStringExtra("artist"); | ||||||
|  |             String album = intent.getStringExtra("album"); | ||||||
|  |             String track = intent.getStringExtra("track"); | ||||||
|  |             Long myPosition = intent.getLongExtra("id", 1); | ||||||
|  |             Log.v("MUSIC", artist + ":" + album + ":" + track); | ||||||
|  |             boolean isRunning = intent.getBooleanExtra("playstate", false) | intent.getBooleanExtra("playing", false); | ||||||
|  | 
 | ||||||
|  |             currentSong.setRunning(isRunning); | ||||||
|  | 
 | ||||||
|  |             if (isRunning == true) { | ||||||
|  |                 currentSong.setAlbum(album); | ||||||
|  |                 currentSong.setArtist(artist); | ||||||
|  |                 currentSong.setTrack(track); | ||||||
|  |                 currentSong.setPosition(myPosition.intValue()); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  |     }; | ||||||
| 
 | 
 | ||||||
|     public RealService() { |     public RealService() { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void sendSerial(String str) { | ||||||
|  | 
 | ||||||
|  |         sendSerial(str, Charset.forName("UTF-8")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private synchronized void sendSerial(String str, Charset enc) { | ||||||
|  | 
 | ||||||
|  |         if (sPort != null) { | ||||||
|  |             try { | ||||||
|  | 
 | ||||||
|  |                 int len = sPort.write(getStringToByte(str, enc), 3000); | ||||||
|  |                 sPort.write(new byte[]{0xd}, 3000); | ||||||
|  |                 Log.d("TAG", str); | ||||||
|  |             } catch (Exception e) { | ||||||
|  |                 showToast(getApplication(), "Error:" + e.toString()); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |         } else { | ||||||
|  |             showToast(getApplication(), "SERIAL NULL"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected byte[] getStringToByte(String str, Charset enc) { | ||||||
|  |         ByteBuffer buf = ByteBuffer.allocate(150); | ||||||
|  | 
 | ||||||
|  |         String[] arrStr = null; | ||||||
|  |         if (str.startsWith("U4PL13") || str.startsWith("U4PL23") || str.startsWith("U4PL33")) { | ||||||
|  |             arrStr = str.split("\\|"); | ||||||
|  |             buf.put(arrStr[0].getBytes(Charset.forName("UTF-8"))); | ||||||
|  |             buf.put(new byte[]{0x7c}); | ||||||
|  |             buf.put(arrStr[1].getBytes(enc)); | ||||||
|  |             buf.put(new byte[]{0x7c}); | ||||||
|  | 
 | ||||||
|  |         } else { | ||||||
|  |             buf.put(str.getBytes(Charset.forName("UTF-8"))); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         byte[] bytes = new byte[buf.position()]; | ||||||
|  |         buf.flip(); | ||||||
|  |         buf.get(bytes); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         return bytes; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public int onStartCommand(Intent intent, int flags, int startId) { |     public int onStartCommand(Intent intent, int flags, int startId) { | ||||||
|         serviceIntent = intent; |         serviceIntent = intent; | ||||||
|         showToast(getApplication(), "Start Service"); |         showToast(getApplication(), "Start Service"); | ||||||
| 
 | 
 | ||||||
| // |  | ||||||
| //        mainThread = new Thread(new Runnable() { |  | ||||||
| //            @Override |  | ||||||
| //            public void run() { |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); |         UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); | ||||||
|         List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager); |         List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager); | ||||||
|         if (availableDrivers.isEmpty()) { |         if (availableDrivers.isEmpty()) { | ||||||
| @ -65,27 +134,29 @@ public class RealService extends Service { | |||||||
|             return START_NOT_STICKY; |             return START_NOT_STICKY; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| // Open a connection to the first available driver. |  | ||||||
|         UsbSerialDriver driver = null; |         UsbSerialDriver driver = null; | ||||||
|         UsbDeviceConnection connection = null; |         UsbDeviceConnection connection = null; | ||||||
|         try { |         try { | ||||||
|             driver = availableDrivers.get(0); |             driver = availableDrivers.get(0); | ||||||
|  | 
 | ||||||
|             connection = manager.openDevice(driver.getDevice()); |             connection = manager.openDevice(driver.getDevice()); | ||||||
|             if (connection == null) { |             if (connection == null) { | ||||||
|                 showToast(getApplication(), "No connection"); |                 showToast(getApplication(), "No connection"); | ||||||
|                         // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..) |  | ||||||
|                 return START_NOT_STICKY; |                 return START_NOT_STICKY; | ||||||
|  | 
 | ||||||
|             } |             } | ||||||
|                 } |             sPort = driver.getPorts().get(0); | ||||||
|                 catch(Exception e) | 
 | ||||||
|                 { | 
 | ||||||
|  |         } catch (Exception e) { | ||||||
|             showToast(getApplication(), e.toString()); |             showToast(getApplication(), e.toString()); | ||||||
|  |             return START_NOT_STICKY; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
|                     sPort.open(connection); |  | ||||||
|                     sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); |  | ||||||
| 
 | 
 | ||||||
|  |             sPort.open(connection); | ||||||
|  |             sPort.setParameters(19200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
| @ -96,38 +167,20 @@ public class RealService extends Service { | |||||||
|                 // Ignore. |                 // Ignore. | ||||||
|             } |             } | ||||||
|             sPort = null; |             sPort = null; | ||||||
|  |             return START_NOT_STICKY; | ||||||
|         } |         } | ||||||
|                 //set the time |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| //                try { |         SimpleDateFormat format1 = new SimpleDateFormat("00yyMMddHHmm"); | ||||||
| //                    sPort.write("zxcv".getBytes(), 50); |         //   String f = format1.format(System.currentTimeMillis()); | ||||||
| //                } |         String currentInfo = format1.format(new Date()); | ||||||
| //                catch(Exception e) |         String currentTime = String.format("TMST%s", currentInfo); | ||||||
| //                { |         sendSerial(currentTime); | ||||||
| // |         sendSerial("U4EN00"); | ||||||
| //                } | 
 | ||||||
| //                SimpleDateFormat sdf = new SimpleDateFormat("aa hh:mm"); |  | ||||||
| //                boolean run = true; |  | ||||||
| //                while (run) { |  | ||||||
| //                    try { |  | ||||||
| //                        Thread.sleep(1000 * 60 * 1); // 1 minute |  | ||||||
| //                        Date date = new Date(); |  | ||||||
| //                        //showToast(getApplication(), sdf.format(date)); |  | ||||||
| //                        sendNotification(sdf.format(date)); |  | ||||||
| //                    } catch (InterruptedException e) { |  | ||||||
| //                        run = false; |  | ||||||
| //                        e.printStackTrace(); |  | ||||||
| //                    } |  | ||||||
| //                } |  | ||||||
| //            } |  | ||||||
| //        }); |  | ||||||
| //        mainThread.start(); |  | ||||||
|         IntentFilter iF = new IntentFilter(); |         IntentFilter iF = new IntentFilter(); | ||||||
|         iF.addAction("com.android.music.metachanged"); |         iF.addAction("com.android.music.metachanged"); | ||||||
| 
 |  | ||||||
|         iF.addAction("com.htc.music.metachanged"); |         iF.addAction("com.htc.music.metachanged"); | ||||||
| 
 |  | ||||||
|         iF.addAction("fm.last.android.metachanged"); |         iF.addAction("fm.last.android.metachanged"); | ||||||
|         iF.addAction("com.sec.android.app.music.metachanged"); |         iF.addAction("com.sec.android.app.music.metachanged"); | ||||||
|         iF.addAction("com.nullsoft.winamp.metachanged"); |         iF.addAction("com.nullsoft.winamp.metachanged"); | ||||||
| @ -138,8 +191,58 @@ public class RealService extends Service { | |||||||
|         iF.addAction("com.rdio.android.metachanged"); |         iF.addAction("com.rdio.android.metachanged"); | ||||||
|         iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged"); |         iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged"); | ||||||
|         iF.addAction("com.andrew.apollo.metachanged"); |         iF.addAction("com.andrew.apollo.metachanged"); | ||||||
|  |         iF.addAction("com.android.music.metachanged"); | ||||||
|  |         iF.addAction("com.android.music.playstatechanged"); | ||||||
|  |         iF.addAction("com.android.music.playbackcomplete"); | ||||||
|  |         iF.addAction("com.android.music.queuechanged"); | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|         registerReceiver(mReceiver, iF); |         registerReceiver(mReceiver, iF); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         new Thread(new Runnable() { | ||||||
|  |             @Override | ||||||
|  |             public void run() { | ||||||
|  | 
 | ||||||
|  |                 boolean myRun = true; | ||||||
|  |                 int stopCount = 0; | ||||||
|  |                 while (myRun) { | ||||||
|  |                     try { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |                         Thread.sleep(5000); | ||||||
|  |                         SimpleDateFormat format1 = new SimpleDateFormat("00yyMMddHHmm"); | ||||||
|  |                         String currentInfo = format1.format(new Date()); | ||||||
|  |                         String currentTime = String.format("TMST%s", currentInfo); | ||||||
|  |                         sendSerial(currentTime); | ||||||
|  | 
 | ||||||
|  |                         if (currentSong.isRunning()) { | ||||||
|  |                             stopCount = 0; | ||||||
|  |                             sendSerial("U4EN00"); | ||||||
|  |                             sendSerial("U4OP00"); | ||||||
|  |                             sendSerial("U4PL03|" + currentSong.getTotal() + "," + currentSong.getPosition() + "|0001010000000"); | ||||||
|  |                             sendSerial("U4PL13|" + currentSong.getAlbum() + "|", Charset.forName("UTF-8")); | ||||||
|  |                             sendSerial("U4PL23|" + currentSong.getTrack() + "|", Charset.forName("UTF-8")); | ||||||
|  |                             sendSerial("U4PL33|" + currentSong.getArtist() + "|", Charset.forName("UTF-8")); | ||||||
|  |                         } else { | ||||||
|  |                             if (stopCount > 5) { | ||||||
|  |                                 sendSerial("U4DI00"); | ||||||
|  |                             } | ||||||
|  |                             stopCount++; | ||||||
|  |                         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |                     } catch (InterruptedException e) { | ||||||
|  |                         myRun = false; | ||||||
|  | 
 | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |         }).start(); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|         return START_NOT_STICKY; |         return START_NOT_STICKY; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -149,19 +252,15 @@ public class RealService extends Service { | |||||||
|         try { |         try { | ||||||
|             sPort.close(); |             sPort.close(); | ||||||
|             sPort = null; |             sPort = null; | ||||||
|         } |         } catch (Exception e) { | ||||||
|         catch(Exception e) |             showToast(getApplication(), "stop Error"); | ||||||
|         { |  | ||||||
| 
 |  | ||||||
|         } |         } | ||||||
|         serviceIntent = null; |         serviceIntent = null; | ||||||
|         setAlarmTimer(); |         setAlarmTimer(); | ||||||
|         Thread.currentThread().interrupt(); |         Thread.currentThread().interrupt(); | ||||||
|  |         unregisterReceiver(mReceiver); | ||||||
|  |         showToast(getApplication(), "stop Service"); | ||||||
| 
 | 
 | ||||||
| //        if (mainThread != null) { |  | ||||||
| //            mainThread.interrupt(); |  | ||||||
| //            mainThread = null; |  | ||||||
| //        } |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -184,7 +283,7 @@ public class RealService extends Service { | |||||||
|         h.post(new Runnable() { |         h.post(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 Toast.makeText(application, msg, Toast.LENGTH_LONG).show(); |                 Toast.makeText(application, msg, Toast.LENGTH_SHORT).show(); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| @ -193,7 +292,7 @@ public class RealService extends Service { | |||||||
|         final Calendar c = Calendar.getInstance(); |         final Calendar c = Calendar.getInstance(); | ||||||
|         c.setTimeInMillis(System.currentTimeMillis()); |         c.setTimeInMillis(System.currentTimeMillis()); | ||||||
|         c.add(Calendar.SECOND, 1); |         c.add(Calendar.SECOND, 1); | ||||||
|         Intent intent = new Intent(this, AlarmRecever.class); |         Intent intent = new Intent(this, AlarmReceiver.class); | ||||||
|         PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); |         PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); | ||||||
| 
 | 
 | ||||||
|         AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); |         AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); | ||||||
| @ -201,62 +300,4 @@ public class RealService extends Service { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     private BroadcastReceiver mReceiver = new BroadcastReceiver() { |  | ||||||
| 
 |  | ||||||
|         @Override |  | ||||||
|         public void onReceive(Context context, Intent intent) { |  | ||||||
|             try { |  | ||||||
|                 if (sPort != null) |  | ||||||
|                 { |  | ||||||
|                     sPort.write("abc".getBytes(), 3000); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             catch(Exception e) |  | ||||||
|             { |  | ||||||
|                 e.toString(); |  | ||||||
|             } |  | ||||||
|             String action = intent.getAction(); |  | ||||||
|             String cmd = intent.getStringExtra("command"); |  | ||||||
|             Log.v("tag ", action + " / " + cmd); |  | ||||||
|             String artist = intent.getStringExtra("artist"); |  | ||||||
|             String album = intent.getStringExtra("album"); |  | ||||||
|             String track = intent.getStringExtra("track"); |  | ||||||
|             Log.v("tag", artist + ":" + album + ":" + track); |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         //    Toast.makeText(MainActivity.this, track, Toast.LENGTH_SHORT).show(); |  | ||||||
|         } |  | ||||||
|     }; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| //    private void sendNotification(String messageBody) { |  | ||||||
| //        Intent intent = new Intent(this, MainActivity.class); |  | ||||||
| //        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |  | ||||||
| //        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); |  | ||||||
| // |  | ||||||
| //        String channelId = "fcm_default_channel";//getString(R.string.default_notification_channel_id); |  | ||||||
| //        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |  | ||||||
| //        NotificationCompat.Builder notificationBuilder = |  | ||||||
| //                new NotificationCompat.Builder(this, channelId) |  | ||||||
| //                        .setSmallIcon(R.mipmap.ic_launcher)//drawable.splash) |  | ||||||
| //                        .setContentTitle("Service test") |  | ||||||
| //                        .setContentText(messageBody) |  | ||||||
| //                        .setAutoCancel(true) |  | ||||||
| //                        .setSound(defaultSoundUri) |  | ||||||
| //                        .setPriority(Notification.PRIORITY_HIGH) |  | ||||||
| //                        .setContentIntent(pendingIntent); |  | ||||||
| // |  | ||||||
| //        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |  | ||||||
| // |  | ||||||
| //        // Since android Oreo notification channel is needed. |  | ||||||
| //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |  | ||||||
| //            NotificationChannel channel = new NotificationChannel(channelId,"Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT); |  | ||||||
| //            notificationManager.createNotificationChannel(channel); |  | ||||||
| //        } |  | ||||||
| // |  | ||||||
| //        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); |  | ||||||
| //    } |  | ||||||
| } | } | ||||||
| @ -5,9 +5,10 @@ import android.content.Context; | |||||||
| import android.content.Intent; | import android.content.Intent; | ||||||
| import android.os.Build; | import android.os.Build; | ||||||
| 
 | 
 | ||||||
| public class RebootRecever extends BroadcastReceiver { | public class RebootReceiver extends BroadcastReceiver { | ||||||
|     @Override |     @Override | ||||||
|     public void onReceive(Context context, Intent intent) { |     public void onReceive(Context context, Intent intent) { | ||||||
|  | 
 | ||||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||||||
|             Intent in = new Intent(context, RestartService.class); |             Intent in = new Intent(context, RestartService.class); | ||||||
|             context.startForegroundService(in); |             context.startForegroundService(in); | ||||||
| @ -9,7 +9,6 @@ import android.content.Intent; | |||||||
| import android.os.Build; | import android.os.Build; | ||||||
| import android.os.IBinder; | import android.os.IBinder; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| import androidx.core.app.NotificationCompat; | import androidx.core.app.NotificationCompat; | ||||||
| 
 | 
 | ||||||
| public class RestartService extends Service { | public class RestartService extends Service { | ||||||
|  | |||||||
| @ -2,6 +2,12 @@ package com.flywithu.carnav; | |||||||
| 
 | 
 | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
|  | import java.nio.charset.Charset; | ||||||
|  | import java.nio.charset.StandardCharsets; | ||||||
|  | import java.text.SimpleDateFormat; | ||||||
|  | import java.time.LocalDateTime; | ||||||
|  | import java.util.Date; | ||||||
|  | 
 | ||||||
| import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -14,4 +20,63 @@ public class ExampleUnitTest { | |||||||
|     public void addition_isCorrect() { |     public void addition_isCorrect() { | ||||||
|         assertEquals(4, 2 + 2); |         assertEquals(4, 2 + 2); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void checkDate() | ||||||
|  |     { | ||||||
|  |         LocalDateTime currentDateTime = LocalDateTime.now(); | ||||||
|  |         //TMST001801140907 | ||||||
|  |         //TMST00YYDDDD0907 | ||||||
|  | 
 | ||||||
|  |         Date myDate = new Date ( "Sun,5 Dec 1999 00:07:21" ); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         SimpleDateFormat format1 = new SimpleDateFormat ( "yy MM dd HH mm"); | ||||||
|  |      //   String f = format1.format(System.currentTimeMillis()); | ||||||
|  |         String f = format1.format(myDate); | ||||||
|  |         System.out.println(f); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  |     private static String bytesToHex(byte[] hashInBytes) { | ||||||
|  | 
 | ||||||
|  |         StringBuilder sb = new StringBuilder(); | ||||||
|  |         for (byte b : hashInBytes) { | ||||||
|  |             sb.append(String.format("%02x", b)); | ||||||
|  |         } | ||||||
|  |         return sb.toString(); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  |     @Test | ||||||
|  |     public void myTest() | ||||||
|  |     { | ||||||
|  |         RealService rs = new RealService(); | ||||||
|  |         rs.getStringToByte("U4PL23|헤어지자 (Prod. 정키)|", Charset.forName("UTF-8")); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void checkout() | ||||||
|  |     { | ||||||
|  |         //55 34 50 4C 33 33 7C ED 9C 98 EC 9D B8 20 28 57 68 65 65 20 49 6E 29 7C 0D | ||||||
|  |         String test = "오"; | ||||||
|  |         //EC A7 80 EC 96 B4 EB B0 98 | ||||||
|  | 
 | ||||||
|  |         //오 | ||||||
|  |         //오 | ||||||
|  |         //        ec98a4 | ||||||
|  |         //feffc624 | ||||||
|  |         //0000c624 | ||||||
|  |         System.out.println(test); | ||||||
|  |         try { | ||||||
|  |             System.out.println(bytesToHex(test.getBytes("utf-8"))); | ||||||
|  |             System.out.println(bytesToHex(test.getBytes("utf-16"))); | ||||||
|  |             System.out.println(bytesToHex(test.getBytes("utf-32"))); | ||||||
|  |             System.out.println(new String(new byte[]{0x0,(byte)0xec,0x0,(byte)0x98,0x0,(byte)0xa4}, StandardCharsets.UTF_16)); | ||||||
|  |         } | ||||||
|  |         catch(Exception e) | ||||||
|  |         { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user