From 039827d078ae9a92f611ab9f6589b1289fd0bbb3 Mon Sep 17 00:00:00 2001 From: SeungheeOh Date: Thu, 5 Sep 2019 23:48:52 +0900 Subject: [PATCH] second commit --- .../java/com/flywithu/carnav/RealService.java | 330 ++++++++---------- .../com/flywithu/carnav/ExampleUnitTest.java | 18 +- 2 files changed, 149 insertions(+), 199 deletions(-) diff --git a/app/src/main/java/com/flywithu/carnav/RealService.java b/app/src/main/java/com/flywithu/carnav/RealService.java index 667b847..5e9e7ff 100644 --- a/app/src/main/java/com/flywithu/carnav/RealService.java +++ b/app/src/main/java/com/flywithu/carnav/RealService.java @@ -1,5 +1,6 @@ package com.flywithu.carnav; +import android.app.AlarmManager; import android.app.Application; import android.app.Notification; import android.app.NotificationChannel; @@ -12,10 +13,12 @@ import android.content.Intent; import android.content.IntentFilter; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbManager; +import android.media.RingtoneManager; +import android.net.Uri; import android.os.Build; -import android.os.Bundle; import android.os.Handler; import android.os.IBinder; + import android.util.Log; import android.widget.Toast; @@ -26,9 +29,8 @@ import com.hoho.android.usbserial.driver.UsbSerialPort; import com.hoho.android.usbserial.driver.UsbSerialProber; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.List; @@ -37,127 +39,41 @@ public class RealService extends Service { public static Intent serviceIntent = null; private static UsbSerialPort sPort = null; - private MusicTitle currentSong = new MusicTitle(); - Boolean oldmethod = true; - 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(); - if (action.startsWith("com.flywithu")) { - oldmethod = false; - } - - if (oldmethod == true) { - 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()); - } - } else { - currentSong.setAlbum(intent.getStringExtra("album")); - currentSong.setArtist(intent.getStringExtra("artist")); - currentSong.setTrack(intent.getStringExtra("track")); - currentSong.setPosition(0); - - - } - - - } - }; - - private void setNotification() { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default"); - builder.setSmallIcon(R.mipmap.ic_launcher); - builder.setContentTitle(null); - builder.setContentText(null); - Intent notificationIntent = new Intent(this, MainActivity.class); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - builder.setContentIntent(pendingIntent); - - NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - manager.createNotificationChannel(new NotificationChannel("default", "기본 채널", NotificationManager.IMPORTANCE_NONE)); - } - - Notification notification = builder.build(); - startForeground(9, notification); - - - } public RealService() { } private void sendSerial(String str) { - - sendSerial(str, Charset.forName("UTF-8")); - } - - private synchronized void sendSerial(String str, Charset enc) { - + byte t1[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x30, 0x33, 0x7C, 0x31, 0x2C, 0x31, 0x7C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}; + byte t2[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x31, 0x33, 0x7C, 0x41, (byte) 0xec, (byte) 0xa7, (byte) 0x80, (byte) 0xec, (byte) 0x96, (byte) 0xb4, (byte) 0xeb, (byte) 0xb0, (byte) 0x98, 0x5a, 0x7c}; + byte t3[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x32, 0x33, 0x7C, (byte) 0xfe, (byte) 0xff, 0x00, 0x41, (byte) 0xc9, (byte) 0xc0, (byte) 0xc5, (byte) 0xb4, (byte) 0xbc, 0x18, 0x00, 0x5a, 0x7c}; + byte t4[] = new byte[]{0x55, 0x34, 0x50, 0x4C, 0x33, 0x33, 0x7C, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, (byte) 0xc9, (byte) 0xc0, 0x00, 0x00, (byte) 0xc5, (byte) 0xb4, 0x00, 0x00, (byte) 0xbc, 0x18, 0x00, 0x00, 0x00, 0x5a, 0x7c}; + /* + 41eca780ec96b4ebb0985a +feff0041c9c0c5b4bc18005a +000000410000c9c00000c5b40000bc180000005a + */ if (sPort != null) { try { - - int len = sPort.write(getStringToByte(str, enc), 3000); + // sPort.write(str.getBytes(), 3000); + sPort.write(t1, 3000); + sPort.write(new byte[]{0xd}, 3000); + + sPort.write(t2, 3000); + sPort.write(new byte[]{0xd}, 3000); + + sPort.write(t3, 3000); + sPort.write(new byte[]{0xd}, 3000); + + sPort.write(t4, 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 @@ -165,6 +81,12 @@ public class RealService extends Service { serviceIntent = intent; showToast(getApplication(), "Start Service"); +// +// mainThread = new Thread(new Runnable() { +// @Override +// public void run() { + + UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); List availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager); if (availableDrivers.isEmpty()) { @@ -172,27 +94,22 @@ public class RealService extends Service { return START_NOT_STICKY; } +// Open a connection to the first available driver. UsbSerialDriver driver = null; UsbDeviceConnection connection = null; try { driver = availableDrivers.get(0); - connection = manager.openDevice(driver.getDevice()); if (connection == null) { showToast(getApplication(), "No connection"); + // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..) return START_NOT_STICKY; - } - sPort = driver.getPorts().get(0); - - } catch (Exception e) { showToast(getApplication(), e.toString()); - return START_NOT_STICKY; } try { - sPort.open(connection); sPort.setParameters(19200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); @@ -205,20 +122,62 @@ public class RealService extends Service { // Ignore. } sPort = null; - return START_NOT_STICKY; } - - + //set the time + //TMST001801140907 + //TMST00YYDDDD0907 + //TMST001205010916 SimpleDateFormat format1 = new SimpleDateFormat("00yyMMddHHmm"); // String f = format1.format(System.currentTimeMillis()); String currentInfo = format1.format(new Date()); String currentTime = String.format("TMST%s", currentInfo); sendSerial(currentTime); sendSerial("U4EN00"); + sendSerial("*R|R1R2R3D1D2A1A3U4|"); + sendSerial("U4PL03|5,3|0001010000000"); + sendSerial("U4PL13|soar| U4PL23|헤어지자 (Prod. 정키)|"); + sendSerial("U4PL33|휘인 (Whee In)|"); + +// sendSerial("U4DI00"); +// sendSerial("*R|R1R2R3D1D2A1A3|"); +// U4EN00 +// *R|R1R2R3D1D2A1A3U4| +// A1PL00 + + + //U4PL03|1,1|0001010000000 U4PL13|soar| U4PL23|헤어지자 (Prod. 정키)| U4PL33|휘인 (Whee In)| + + showToast(getApplication(), currentTime); + +// try { +// sPort.write("zxcv".getBytes(), 50); +// } +// catch(Exception e) +// { +// +// } +// 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(); iF.addAction("com.android.music.metachanged"); + iF.addAction("com.htc.music.metachanged"); + iF.addAction("fm.last.android.metachanged"); iF.addAction("com.sec.android.app.music.metachanged"); iF.addAction("com.nullsoft.winamp.metachanged"); @@ -229,62 +188,9 @@ public class RealService extends Service { iF.addAction("com.rdio.android.metachanged"); iF.addAction("com.samsung.sec.android.MusicPlayer.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"); - iF.addAction("com.flywithu.carnav.metachanged"); - - - 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_STICKY; + return START_NOT_STICKY; } @Override @@ -294,22 +200,21 @@ public class RealService extends Service { sPort.close(); sPort = null; } catch (Exception e) { - showToast(getApplication(), "stop Error"); + } serviceIntent = null; + setAlarmTimer(); Thread.currentThread().interrupt(); - unregisterReceiver(mReceiver); - showToast(getApplication(), "stop Service"); +// if (mainThread != null) { +// mainThread.interrupt(); +// mainThread = null; +// } } @Override public void onCreate() { super.onCreate(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - setNotification(); - - } } @Override @@ -327,12 +232,73 @@ public class RealService extends Service { h.post(new Runnable() { @Override public void run() { - Toast.makeText(application, msg, Toast.LENGTH_SHORT).show(); + Toast.makeText(application, msg, Toast.LENGTH_LONG).show(); } }); } + protected void setAlarmTimer() { + final Calendar c = Calendar.getInstance(); + c.setTimeInMillis(System.currentTimeMillis()); + c.add(Calendar.SECOND, 1); + Intent intent = new Intent(this, AlarmRecever.class); + PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); + + AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + mAlarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender); + } + 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()); +// } } \ No newline at end of file diff --git a/app/src/test/java/com/flywithu/carnav/ExampleUnitTest.java b/app/src/test/java/com/flywithu/carnav/ExampleUnitTest.java index 2cb65a7..6a29cb7 100644 --- a/app/src/test/java/com/flywithu/carnav/ExampleUnitTest.java +++ b/app/src/test/java/com/flywithu/carnav/ExampleUnitTest.java @@ -2,8 +2,6 @@ package com.flywithu.carnav; 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; @@ -47,31 +45,17 @@ public class ExampleUnitTest { 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 = "오"; + String test = "A지어반Z"; //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) {