From f84e4684020cd6b95fd6c2de1d34c3d0bf257a19 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 26 Nov 2018 01:56:43 +0100 Subject: [PATCH] remove unused functionality from KeyCachingService --- AndroidManifest.xml | 2 - .../securesms/service/KeyCachingService.java | 320 +----------------- 2 files changed, 2 insertions(+), 320 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5cb3920cd..2c856ae1a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -270,8 +270,6 @@ android:theme="@style/TextSecure.LightTheme" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/> - - () { - @Override - protected Void doInBackground(Void... params) { - if (!DatabaseUpgradeActivity.isUpdate(KeyCachingService.this)) { - MessageNotifier.updateNotification(KeyCachingService.this); - } - return null; - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (intent == null) return START_NOT_STICKY; - Log.w("KeyCachingService", "onStartCommand, " + intent.getAction()); - - if (intent.getAction() != null) { - switch (intent.getAction()) { - case CLEAR_KEY_ACTION: handleClearKey(); break; - case ACTIVITY_START_EVENT: handleActivityStarted(); break; - case ACTIVITY_STOP_EVENT: handleActivityStopped(); break; - case PASSPHRASE_EXPIRED_EVENT: handleClearKey(); break; - case DISABLE_ACTION: handleDisableService(); break; - case LOCALE_CHANGE_EVENT: handleLocaleChanged(); break; - case LOCK_TOGGLED_EVENT: handleLockToggled(); break; - } - } - - return START_NOT_STICKY; - } - - @Override - public void onCreate() { - Log.w("KeyCachingService", "onCreate()"); - super.onCreate(); - this.pending = PendingIntent.getService(this, 0, new Intent(PASSPHRASE_EXPIRED_EVENT, null, - this, KeyCachingService.class), 0); - - if (TextSecurePreferences.isPasswordDisabled(this) && !TextSecurePreferences.isScreenLockEnabled(this)) { - try { - MasterSecret masterSecret = MasterSecretUtil.getMasterSecret(this, MasterSecretUtil.UNENCRYPTED_PASSPHRASE); - setMasterSecret(masterSecret); - } catch (InvalidPassphraseException e) { - Log.w("KeyCachingService", e); - } - } - } - - @Override - public void onDestroy() { - super.onDestroy(); - Log.w("KeyCachingService", "KCS Is Being Destroyed!"); - handleClearKey(); - } - - /** - * Workaround for Android bug: - * https://code.google.com/p/android/issues/detail?id=53313 - */ - @Override - public void onTaskRemoved(Intent rootIntent) { - Intent intent = new Intent(this, DummyActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - } - - private void handleActivityStarted() { - Log.w("KeyCachingService", "Incrementing activity count..."); - - AlarmManager alarmManager = ServiceUtil.getAlarmManager(this); - alarmManager.cancel(pending); - activitiesRunning++; - } - - private void handleActivityStopped() { - Log.w("KeyCachingService", "Decrementing activity count..."); - - activitiesRunning--; - startTimeoutIfAppropriate(); - } - - @SuppressLint("StaticFieldLeak") - private void handleClearKey() { - Log.w("KeyCachingService", "handleClearKey()"); - KeyCachingService.masterSecret = null; - stopForeground(true); - - Intent intent = new Intent(CLEAR_KEY_EVENT); - intent.setPackage(getApplicationContext().getPackageName()); - - sendBroadcast(intent, KEY_PERMISSION); - - new AsyncTask() { - @Override - protected Void doInBackground(Void... params) { - MessageNotifier.updateNotification(KeyCachingService.this); - return null; - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - - private void handleLockToggled() { - stopForeground(true); - - try { - MasterSecret masterSecret = MasterSecretUtil.getMasterSecret(this, MasterSecretUtil.UNENCRYPTED_PASSPHRASE); - setMasterSecret(masterSecret); - } catch (InvalidPassphraseException e) { - Log.w(TAG, e); - } - } - - private void handleDisableService() { - if (TextSecurePreferences.isPasswordDisabled(this) && - !TextSecurePreferences.isScreenLockEnabled(this)) - { - stopForeground(true); - } - } - - private void handleLocaleChanged() { - dynamicLanguage.updateServiceLocale(this); - foregroundService(); - } - - private void startTimeoutIfAppropriate() { - boolean timeoutEnabled = TextSecurePreferences.isScreenLockTimeoutEnabled(this); - long screenTimeout = TextSecurePreferences.getScreenLockTimeout(this); - - if ((activitiesRunning == 0) && (KeyCachingService.masterSecret != null) && - (timeoutEnabled && !TextSecurePreferences.isPasswordDisabled(this)) || - (screenTimeout >= 60 && TextSecurePreferences.isScreenLockEnabled(this))) - { - long passphraseTimeoutMinutes = TextSecurePreferences.getScreenLockTimeoutInterval(this); - long screenLockTimeoutSeconds = TextSecurePreferences.getScreenLockTimeout(this); - - long timeoutMillis; - - if (!TextSecurePreferences.isPasswordDisabled(this)) timeoutMillis = TimeUnit.MINUTES.toMillis(passphraseTimeoutMinutes); - else timeoutMillis = TimeUnit.SECONDS.toMillis(screenLockTimeoutSeconds); - - Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis); - - AlarmManager alarmManager = ServiceUtil.getAlarmManager(this); - alarmManager.cancel(pending); - alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending); - } - } - - @TargetApi(Build.VERSION_CODES.JELLY_BEAN) - private void foregroundServiceModern() { - Log.w("KeyCachingService", "foregrounding KCS"); - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - - builder.setContentTitle(getString(R.string.KeyCachingService_passphrase_cached)); - builder.setContentText(getString(R.string.KeyCachingService_signal_passphrase_cached)); - builder.setSmallIcon(R.drawable.icon_cached); - builder.setWhen(0); - builder.setPriority(Notification.PRIORITY_MIN); - - builder.addAction(R.drawable.ic_menu_lock_dark, getString(R.string.KeyCachingService_lock), buildLockIntent()); - builder.setContentIntent(buildLaunchIntent()); - - stopForeground(true); - startForeground(SERVICE_RUNNING_ID, builder.build()); - } - - private void foregroundServiceICS() { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.key_caching_notification); - - remoteViews.setOnClickPendingIntent(R.id.lock_cache_icon, buildLockIntent()); - - builder.setSmallIcon(R.drawable.icon_cached); - builder.setContent(remoteViews); - builder.setContentIntent(buildLaunchIntent()); - - stopForeground(true); - startForeground(SERVICE_RUNNING_ID, builder.build()); - } - - private void foregroundServiceLegacy() { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - builder.setSmallIcon(R.drawable.icon_cached); - builder.setWhen(System.currentTimeMillis()); - - builder.setContentTitle(getString(R.string.KeyCachingService_passphrase_cached)); - builder.setContentText(getString(R.string.KeyCachingService_signal_passphrase_cached)); - builder.setContentIntent(buildLaunchIntent()); - - stopForeground(true); - startForeground(SERVICE_RUNNING_ID, builder.build()); - } - - private void foregroundService() { - if (TextSecurePreferences.isPasswordDisabled(this) && !TextSecurePreferences.isScreenLockEnabled(this)) { - stopForeground(true); - return; - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - foregroundServiceModern(); - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - foregroundServiceICS(); - } else { - foregroundServiceLegacy(); - } - } - - private void broadcastNewSecret() { - Log.w("service", "Broadcasting new secret..."); - - Intent intent = new Intent(NEW_KEY_EVENT); - intent.setPackage(getApplicationContext().getPackageName()); - - sendBroadcast(intent, KEY_PERMISSION); - } - - private PendingIntent buildLockIntent() { - Intent intent = new Intent(this, KeyCachingService.class); - intent.setAction(PASSPHRASE_EXPIRED_EVENT); - return PendingIntent.getService(getApplicationContext(), 0, intent, 0); - } - - private PendingIntent buildLaunchIntent() { - Intent intent = new Intent(this, ConversationListActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - return PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); - } - - @Override - public IBinder onBind(Intent arg0) { - return binder; - } - - public class KeySetBinder extends Binder { - public KeyCachingService getService() { - return KeyCachingService.this; - } + return null; } public static void registerPassphraseActivityStarted(Context activity) { - Intent intent = new Intent(activity, KeyCachingService.class); - intent.setAction(KeyCachingService.ACTIVITY_START_EVENT); - activity.startService(intent); } public static void registerPassphraseActivityStopped(Context activity) { - Intent intent = new Intent(activity, KeyCachingService.class); - intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT); - activity.startService(intent); } }