From 4f96ade68991ead18b3731d9c8de66e72c5530c2 Mon Sep 17 00:00:00 2001 From: Pishang Ujeniya Date: Sun, 21 Oct 2018 01:56:40 +0530 Subject: [PATCH] Redesigned the ControlsActivity Chnaged the Color Theme Redesigned the ControlActivity Layout Updated Login and Signup Activity Layouts SignalRService made ForegroundService with Notificaiton Actions --- ClipSyncAndroid/app/build.gradle | 4 +- ClipSyncAndroid/app/release/output.json | 2 +- .../app/src/main/AndroidManifest.xml | 9 +- .../clipsync/ControlsActivity.java | 59 ++----- .../pishangujeniya/clipsync/GlobalValues.java | 6 + .../clipsync/service/ClipBoardMonitor.java | 2 +- .../clipsync/service/SignalRService.java | 167 +++++++++++++++--- .../src/main/res/drawable/button_rounded.xml | 19 ++ .../ic_power_settings_new_white_24dp.xml | 5 + .../src/main/res/layout/activity_controls.xml | 107 +++++------ .../src/main/res/layout/activity_login.xml | 10 +- .../src/main/res/layout/activity_sign_up.xml | 11 +- .../app/src/main/res/values/colors.xml | 20 ++- .../app/src/main/res/values/styles.xml | 2 +- 14 files changed, 275 insertions(+), 148 deletions(-) create mode 100644 ClipSyncAndroid/app/src/main/res/drawable/button_rounded.xml create mode 100644 ClipSyncAndroid/app/src/main/res/drawable/ic_power_settings_new_white_24dp.xml diff --git a/ClipSyncAndroid/app/build.gradle b/ClipSyncAndroid/app/build.gradle index e02e8ff..1df3e91 100644 --- a/ClipSyncAndroid/app/build.gradle +++ b/ClipSyncAndroid/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId "com.pishangujeniya.clipsync" minSdkVersion 23 targetSdkVersion 28 - versionCode 14 - versionName "1.4" + versionCode 15 + versionName "1.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true } diff --git a/ClipSyncAndroid/app/release/output.json b/ClipSyncAndroid/app/release/output.json index b36a63d..46b48a0 100644 --- a/ClipSyncAndroid/app/release/output.json +++ b/ClipSyncAndroid/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"x86"}],"versionCode":14,"versionName":"1.4","enabled":true,"filterName":"x86","outputFile":"app-x86-release.apk","fullName":"x86Release","baseName":"x86-release"},"path":"app-x86-release.apk","properties":{}},{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"armeabi-v7a"}],"versionCode":14,"versionName":"1.4","enabled":true,"filterName":"armeabi-v7a","outputFile":"app-armeabi-v7a-release.apk","fullName":"armeabi-v7aRelease","baseName":"armeabi-v7a-release"},"path":"app-armeabi-v7a-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"x86"}],"versionCode":15,"versionName":"1.5","enabled":true,"filterName":"x86","outputFile":"app-x86-release.apk","fullName":"x86Release","baseName":"x86-release"},"path":"app-x86-release.apk","properties":{}},{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"armeabi-v7a"}],"versionCode":15,"versionName":"1.5","enabled":true,"filterName":"armeabi-v7a","outputFile":"app-armeabi-v7a-release.apk","fullName":"armeabi-v7aRelease","baseName":"armeabi-v7a-release"},"path":"app-armeabi-v7a-release.apk","properties":{}}] \ No newline at end of file diff --git a/ClipSyncAndroid/app/src/main/AndroidManifest.xml b/ClipSyncAndroid/app/src/main/AndroidManifest.xml index 4add504..e15e7db 100644 --- a/ClipSyncAndroid/app/src/main/AndroidManifest.xml +++ b/ClipSyncAndroid/app/src/main/AndroidManifest.xml @@ -34,13 +34,18 @@ + android:label="Clipboard Monitor" + /> + android:label="ClipSync SignalR" + + /> + + \ No newline at end of file diff --git a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/ControlsActivity.java b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/ControlsActivity.java index e068b60..12ca3d6 100644 --- a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/ControlsActivity.java +++ b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/ControlsActivity.java @@ -3,6 +3,7 @@ import android.app.ActivityManager; import android.content.ClipboardManager; import android.content.Intent; +import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; @@ -17,11 +18,11 @@ public class ControlsActivity extends AppCompatActivity { - private TextView status_text_veiw; - private Button start_service_button; - private Button stop_service_button; - private Button logout_button; - private Button refresh_button; + + private FloatingActionButton start_service_button; + private FloatingActionButton stop_service_button; + private FloatingActionButton logout_button; + private Utility utility; private final String TAG = ControlsActivity.class.getSimpleName(); @@ -31,29 +32,26 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_controls); - start_service_button = findViewById(R.id.activity_controls_service_start_button); - stop_service_button = findViewById(R.id.activity_controls_service_stop_button); - logout_button = findViewById(R.id.activity_controls_log_out_button); - status_text_veiw = findViewById(R.id.activity_controls_status_text_view); - refresh_button = findViewById(R.id.activity_controls_status_refresh_button); - - refresh_button.setVisibility(View.INVISIBLE); - status_text_veiw.setVisibility(View.INVISIBLE); utility = new Utility(this); + start_service_button = findViewById(R.id.activity_controls_service_start_fab); + stop_service_button = findViewById(R.id.activity_controls_service_stop_fab); + logout_button = findViewById(R.id.activity_controls_log_out_fab); + start_service_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent signalRServiceIntent = new Intent(ControlsActivity.this, SignalRService.class); + signalRServiceIntent.setAction(GlobalValues.START_SERVICE); startService(signalRServiceIntent); // Always Call after SignalR Service Started startService(new Intent(ControlsActivity.this, ClipBoardMonitor.class)); - updateStatusText(); - start_service_button.setVisibility(View.INVISIBLE); + start_service_button.hide(); + stop_service_button.show(); } }); @@ -61,18 +59,11 @@ public void onClick(View view) { stop_service_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - stopServices(); - - updateStatusText(); - start_service_button.setVisibility(View.VISIBLE); - } - }); + stop_service_button.hide(); + start_service_button.show(); - refresh_button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - updateStatusText(); + stopServices(); } }); @@ -90,8 +81,8 @@ private boolean isServiceRunning(String classgetName) { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); assert manager != null; for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { - Log.e(TAG,service.service.getClassName()); - Log.e(TAG,"ClassName" +service.service.getClassName()); + Log.e(TAG, service.service.getClassName()); + Log.e(TAG, "ClassName" + service.service.getClassName()); if (classgetName.equals(service.service.getClassName())) { return true; } @@ -101,28 +92,16 @@ private boolean isServiceRunning(String classgetName) { private void stopServices() { Intent signalRServiceIntent = new Intent(ControlsActivity.this, SignalRService.class); + signalRServiceIntent.setAction(GlobalValues.STOP_SERVICE); stopService(signalRServiceIntent); // Always Call after SignalR Service Started stopService(new Intent(ControlsActivity.this, ClipBoardMonitor.class)); } - private void updateStatusText(){ - String text_to_display = "Status : "; - - if (isServiceRunning(SignalRService.class.getName()) && isServiceRunning(ClipboardManager.class.getName())) { - text_to_display = text_to_display + "Yes"; - } else { - text_to_display = text_to_display + "No"; - } - - status_text_veiw.setText(text_to_display); - } - @Override protected void onResume() { super.onResume(); - updateStatusText(); } private void logout() { diff --git a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/GlobalValues.java b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/GlobalValues.java index 5aa0863..533d3fa 100644 --- a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/GlobalValues.java +++ b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/GlobalValues.java @@ -8,5 +8,11 @@ public class GlobalValues { public static String send_copied_text_signalr_method_name = "SendCopiedText"; public static String copied_water_mark = "- Copied By ClipSync"; + public static String STOP_SERVICE = "STOP_SERVICE"; + public static String START_SERVICE = "START_SERVICE"; + + public static int SIGNALR_SERVICE_NOTIFICATION_ID = 1001; + + } diff --git a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/ClipBoardMonitor.java b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/ClipBoardMonitor.java index b0cce4c..f0257d5 100644 --- a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/ClipBoardMonitor.java +++ b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/ClipBoardMonitor.java @@ -75,7 +75,7 @@ public void onServiceConnected(ComponentName className, IBinder service) { Log.e(TAG, "Inside service connected - Activity "); // We've bound to SignalRService, cast the IBinder and get SignalRService instance SignalRService.LocalBinder binder = (SignalRService.LocalBinder) service; - mService = binder.getService(); + mService = (SignalRService) binder.getService(); mBound = true; Log.e(TAG, "bound status - " + mBound); } diff --git a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/SignalRService.java b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/SignalRService.java index 412096f..033bfa9 100644 --- a/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/SignalRService.java +++ b/ClipSyncAndroid/app/src/main/java/com/pishangujeniya/clipsync/service/SignalRService.java @@ -1,26 +1,32 @@ package com.pishangujeniya.clipsync.service; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.os.Binder; import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.Looper; +import android.support.v4.app.NotificationCompat; import android.util.Log; import com.google.gson.JsonElement; +import com.pishangujeniya.clipsync.ControlsActivity; import com.pishangujeniya.clipsync.GlobalValues; +import com.pishangujeniya.clipsync.R; import com.pishangujeniya.clipsync.helper.Utility; -import java.text.SimpleDateFormat; -import java.util.Calendar; - import microsoft.aspnet.signalr.client.Action; +import microsoft.aspnet.signalr.client.ConnectionState; import microsoft.aspnet.signalr.client.ErrorCallback; import microsoft.aspnet.signalr.client.LogLevel; import microsoft.aspnet.signalr.client.Logger; @@ -39,6 +45,17 @@ public class SignalRService extends Service { public Boolean is_service_connected = false; private Context context; + private NotificationManager mNotificationManager; + private Notification notification; + private String CHANNEL_ID = "ClipSyncServer";// The id of the channel. + private CharSequence name = "ClipSyncServer";// The user-visible name of the channel. + private String NOTIFICATION_TITLE = "ClipSync Working"; + private String NOTIFICATION_CONTENT_TEXT = "Copy Paste"; + private PendingIntent pStopSelf; + private Bitmap icon; + private PendingIntent pendingIntent; + + private NotificationChannel mChannel = null; private Utility utility; @@ -55,6 +72,8 @@ public void onCreate() { // context = this.getApplicationContext(); context = getBaseContext(); + mNotificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); utility = new Utility(context); @@ -65,15 +84,35 @@ public void onCreate() { @Override public int onStartCommand(Intent intent, int flags, int startId) { - Log.d("service", "service start - service"); - int result = super.onStartCommand(intent, flags, startId); - startSignalR(); - return result; + if (GlobalValues.STOP_SERVICE.equals(intent.getAction())) { + Log.d(TAG, "called to cancel service"); + stopForeground(true); + stopSelf(); + mNotificationManager.cancel(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID); + } else if (GlobalValues.START_SERVICE.equals(intent.getAction())) { + showNotification(); + startSignalR(); + } + return START_STICKY; } @Override public void onDestroy() { - conn.stop(); + if (conn != null) { + + try { + ConnectionState state = conn.getState(); + if (state.compareTo(ConnectionState.Connected) > -1) { + conn.stop(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + if (mNotificationManager != null) { + mNotificationManager.cancel(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID); + } super.onDestroy(); } @@ -87,7 +126,7 @@ public boolean onUnbind(Intent intent) { public IBinder onBind(Intent intent) { // Return the communication channel to the service. Log.d("service", "onBind - service"); - startSignalR(); +// startSignalR(); return mBinder; } @@ -102,13 +141,46 @@ public SignalRService getService() { } } - /** - * method for clients (activities) - */ - private void getIncommingcht() { - Log.d("Inside : ", "getIncommingcht - service - Method"); -// mHubProxy.invoke("addGroup", ProileId, Copanyid, "true", Token); -// mHubProxy.invoke("GetChatQueue",ProileId, Token); + private void showNotification() { + + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + int importance = NotificationManager.IMPORTANCE_HIGH; + mChannel = new NotificationChannel(CHANNEL_ID, name, importance); + mNotificationManager.createNotificationChannel(mChannel); + } + + Intent notificationIntent = new Intent(this, ControlsActivity.class); + + notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + + pendingIntent = PendingIntent.getActivity(this, 0, + notificationIntent, 0); + + icon = BitmapFactory.decodeResource(getResources(), + R.drawable.clip_sync_logo_2); + + Intent stop_self_intent = new Intent(SignalRService.this, SignalRService.class); + stop_self_intent.setAction(GlobalValues.STOP_SERVICE); + + pStopSelf = PendingIntent.getService(context, GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID, stop_self_intent, PendingIntent.FLAG_CANCEL_CURRENT); + + notification = new NotificationCompat.Builder(this) + .setContentTitle(NOTIFICATION_TITLE) + .setTicker(NOTIFICATION_TITLE) + .setContentText(NOTIFICATION_CONTENT_TEXT) + .setSmallIcon(R.drawable.clip_sync_logo_2) + .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) + .setContentIntent(pendingIntent) + .setOngoing(true) + .setOnlyAlertOnce(true) + .setChannelId(CHANNEL_ID) + .addAction(android.R.drawable.ic_media_previous, "Stop", pStopSelf) + .build(); + + + startForeground(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID, notification); + } public void sendCopiedText(String text) { @@ -117,16 +189,9 @@ public void sendCopiedText(String text) { mHubProxy.invoke(GlobalValues.send_copied_text_signalr_method_name, text); } else { Log.e(TAG, "Service is not connected so not sending copied text"); - } } - public void selectVisitor(String visitor_id, String CompanyID, String DisplayName, String startTime) { - String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); - mHubProxy.invoke("seleVisitr", DisplayName, timeStamp, startTime); - } - - private void startSignalR() { // Create a new console logger @@ -143,7 +208,6 @@ public void log(String message, LogLevel level) { // Create the hub proxy HubProxy proxy = conn.createHubProxy(GlobalValues.SignalHubName); - mHubProxy = proxy; Subscription subscription = proxy.subscribe(GlobalValues.receive_copied_text_signalr_method_name); @@ -204,8 +268,25 @@ public void onError(Throwable error) { @Override public void run() { - System.out.println("CONNECTED"); + System.out.println("Connecting..."); is_service_connected = true; + + if (mNotificationManager != null && notification != null) { + notification = new NotificationCompat.Builder(context) + .setContentTitle(NOTIFICATION_TITLE) + .setTicker(NOTIFICATION_TITLE) + .setContentText("Connecting...") + .setSmallIcon(R.drawable.clip_sync_logo_2) + .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) + .setContentIntent(pendingIntent) + .setOngoing(true) + .setOnlyAlertOnce(true) + .setChannelId(CHANNEL_ID) + .addAction(android.R.drawable.ic_media_previous, "Stop", pStopSelf) + .build(); + + mNotificationManager.notify(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID, notification); + } } }); @@ -215,6 +296,22 @@ public void run() { @Override public void run() { System.out.println("DISCONNECTED"); + if (mNotificationManager != null && notification != null) { + notification = new NotificationCompat.Builder(context) + .setContentTitle(NOTIFICATION_TITLE) + .setTicker(NOTIFICATION_TITLE) + .setContentText("Disconnected") + .setSmallIcon(R.drawable.clip_sync_logo_2) + .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + .setOnlyAlertOnce(true) + .setChannelId(CHANNEL_ID) + .addAction(android.R.drawable.ic_media_previous, "Stop", pStopSelf) + .build(); + + mNotificationManager.notify(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID, notification); + } } }); @@ -222,7 +319,23 @@ public void run() { conn.start().done(new Action() { @Override public void run(Void obj) { - System.out.println("Done Connecting!"); + System.out.println("Connected"); + if (mNotificationManager != null && notification != null) { + notification = new NotificationCompat.Builder(context) + .setContentTitle(NOTIFICATION_TITLE) + .setTicker(NOTIFICATION_TITLE) + .setContentText("Connected") + .setSmallIcon(R.drawable.clip_sync_logo_2) + .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) + .setContentIntent(pendingIntent) + .setOngoing(true) + .setOnlyAlertOnce(true) + .setChannelId(CHANNEL_ID) + .addAction(android.R.drawable.ic_media_previous, "Stop", pStopSelf) + .build(); + + mNotificationManager.notify(GlobalValues.SIGNALR_SERVICE_NOTIFICATION_ID, notification); + } } }); diff --git a/ClipSyncAndroid/app/src/main/res/drawable/button_rounded.xml b/ClipSyncAndroid/app/src/main/res/drawable/button_rounded.xml new file mode 100644 index 0000000..5e4d3a4 --- /dev/null +++ b/ClipSyncAndroid/app/src/main/res/drawable/button_rounded.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ClipSyncAndroid/app/src/main/res/drawable/ic_power_settings_new_white_24dp.xml b/ClipSyncAndroid/app/src/main/res/drawable/ic_power_settings_new_white_24dp.xml new file mode 100644 index 0000000..1867b8c --- /dev/null +++ b/ClipSyncAndroid/app/src/main/res/drawable/ic_power_settings_new_white_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/ClipSyncAndroid/app/src/main/res/layout/activity_controls.xml b/ClipSyncAndroid/app/src/main/res/layout/activity_controls.xml index 9e0c573..069e467 100644 --- a/ClipSyncAndroid/app/src/main/res/layout/activity_controls.xml +++ b/ClipSyncAndroid/app/src/main/res/layout/activity_controls.xml @@ -1,73 +1,60 @@ - - - - - - - -