mirror of
https://github.com/wesaphzt/privatelock.git
synced 2025-04-21 06:59:09 +00:00
fix to stop service correctly preventing mem leak & code cleanup
This commit is contained in:
parent
4d67f4dd47
commit
53828ddc3f
4 changed files with 117 additions and 126 deletions
|
@ -37,7 +37,6 @@ import com.wesaphzt.privatelock.fragments.FragmentAbout;
|
|||
import com.wesaphzt.privatelock.fragments.FragmentDonate;
|
||||
import com.wesaphzt.privatelock.fragments.FragmentSettings;
|
||||
import com.wesaphzt.privatelock.receivers.DeviceAdminReceiver;
|
||||
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
||||
import com.wesaphzt.privatelock.service.LockService;
|
||||
import com.wesaphzt.privatelock.widget.LockWidgetProvider;
|
||||
|
||||
|
@ -291,27 +290,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
private void startServicePrep() {
|
||||
//stop this listener
|
||||
mSensorManager.unregisterListener(mActiveListener);
|
||||
Intent intent = new Intent(context, LockService.class);
|
||||
|
||||
try {
|
||||
//stop any already running service
|
||||
LockService.mSensorManager.unregisterListener(LockService.activeListener);
|
||||
startLockService(intent);
|
||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
||||
lockWidgetProvider.setWidgetStart(context);
|
||||
//start service intent
|
||||
Intent startIntent = new Intent(context, LockService.class);
|
||||
startIntent.setAction(LockService.ACTION_START_FOREGROUND_SERVICE);
|
||||
|
||||
//cancel any pause timer that might be running
|
||||
try {
|
||||
PauseReceiver.mCountdown.cancel();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
startLockService(intent);
|
||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
||||
lockWidgetProvider.setWidgetStart(context);
|
||||
}
|
||||
startLockService(startIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
package com.wesaphzt.privatelock.receivers;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.wesaphzt.privatelock.service.LockService;
|
||||
import com.wesaphzt.privatelock.widget.LockWidgetProvider;
|
||||
import com.wesaphzt.privatelock.R;
|
||||
|
||||
import static com.wesaphzt.privatelock.service.LockService.activeListener;
|
||||
import static com.wesaphzt.privatelock.service.LockService.mSensorManager;
|
||||
import static com.wesaphzt.privatelock.service.LockService.disabled;
|
||||
|
||||
public class NotificationReceiver extends BroadcastReceiver {
|
||||
|
||||
|
@ -23,37 +14,13 @@ public class NotificationReceiver extends BroadcastReceiver {
|
|||
String action = intent.getStringExtra("lock_service");
|
||||
|
||||
if (action.equals("lock_service_notification")) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
||||
Intent stopIntent = new Intent(context, LockService.class);
|
||||
stopIntent.setAction(LockService.ACTION_STOP_FOREGROUND_SERVICE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
String id = context.getString(R.string.notification_main_channel_id);
|
||||
notificationManager.deleteNotificationChannel(id);
|
||||
if(PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
} else {
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
}
|
||||
|
||||
context.startForegroundService(stopIntent);
|
||||
} else {
|
||||
notificationManager.cancel(LockService.NOTIFICATION_ID);
|
||||
if(PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
} else {
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
}
|
||||
context.startService(stopIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import com.wesaphzt.privatelock.receivers.DeviceAdminReceiver;
|
|||
import com.wesaphzt.privatelock.receivers.NotificationReceiver;
|
||||
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
||||
import com.wesaphzt.privatelock.receivers.PresenceReceiver;
|
||||
import com.wesaphzt.privatelock.widget.LockWidgetProvider;
|
||||
|
||||
import static androidx.core.app.NotificationCompat.PRIORITY_LOW;
|
||||
import static androidx.core.app.NotificationCompat.PRIORITY_MIN;
|
||||
|
@ -67,53 +68,115 @@ public class LockService extends JobIntentService {
|
|||
public static final int DEFAULT_SENSITIVITY = 10;
|
||||
public static int SENSITIVITY;
|
||||
|
||||
public static final String ACTION_STOP_FOREGROUND_SERVICE = "ACTION_STOP_FOREGROUND_SERVICE";
|
||||
public static final String ACTION_START_FOREGROUND_SERVICE = "ACTION_START_FOREGROUND_SERVICE";
|
||||
|
||||
PresenceReceiver presenceReceiver;
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
context = getApplicationContext();
|
||||
CHANNEL_ID = getString(R.string.notification_main_channel_id);
|
||||
CHANNEL_NAME = getString(R.string.notification_main_channel_name);
|
||||
//------------------------------------------------------------------------------------------
|
||||
PresenceReceiver presenceReceiver = new PresenceReceiver();
|
||||
if (intent != null) {
|
||||
String action = intent.getAction();
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_PRESENT);
|
||||
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
registerReceiver(presenceReceiver, intentFilter);
|
||||
//------------------------------------------------------------------------------------------
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
|
||||
try {
|
||||
SENSITIVITY = prefs.getInt(MainActivity.PREFS_THRESHOLD, DEFAULT_SENSITIVITY);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(context, "Unable to retrieve threshold", Toast.LENGTH_LONG).show();
|
||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
||||
|
||||
}
|
||||
//------------------------------------------------------------------------------------------
|
||||
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
switch (action) {
|
||||
|
||||
//dpm
|
||||
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
mDeviceAdmin = new ComponentName(this, DeviceAdminReceiver.class);
|
||||
case ACTION_START_FOREGROUND_SERVICE:
|
||||
presenceReceiver = new PresenceReceiver();
|
||||
|
||||
//prevent lock animation artifacts
|
||||
mInitialized = false;
|
||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_PRESENT);
|
||||
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
registerReceiver(presenceReceiver, intentFilter);
|
||||
|
||||
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
//------------------------------------------------------------------------------------------
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
|
||||
try {
|
||||
SENSITIVITY = prefs.getInt(MainActivity.PREFS_THRESHOLD, DEFAULT_SENSITIVITY);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(context, "Unable to retrieve threshold", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
//------------------------------------------------------------------------------------------
|
||||
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
setSensorListener();
|
||||
mSensorManager.registerListener(activeListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
//dpm
|
||||
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
mDeviceAdmin = new ComponentName(this, DeviceAdminReceiver.class);
|
||||
|
||||
setNotification();
|
||||
//------------------------------------------------------------------------------------------
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
//create foreground service
|
||||
startForeground(NOTIFICATION_ID, notification);
|
||||
disabled = false;
|
||||
//prevent lock animation artifacts
|
||||
mInitialized = false;
|
||||
|
||||
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
|
||||
setSensorListener();
|
||||
mSensorManager.registerListener(activeListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
|
||||
setNotification();
|
||||
//------------------------------------------------------------------------------------------
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
//create foreground service
|
||||
startForeground(NOTIFICATION_ID, notification);
|
||||
lockWidgetProvider.setWidgetStart(context);
|
||||
disabled = false;
|
||||
|
||||
} else {
|
||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||
lockWidgetProvider.setWidgetStart(context);
|
||||
disabled = false;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ACTION_STOP_FOREGROUND_SERVICE:
|
||||
try {
|
||||
unregisterReceiver(presenceReceiver);
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
|
||||
disabled = true;
|
||||
mInitialized = false;
|
||||
|
||||
if (PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel();
|
||||
PauseReceiver.isRunning = false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
disabled = true;
|
||||
mInitialized = false;
|
||||
|
||||
if (PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel();
|
||||
PauseReceiver.isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
String id = context.getString(R.string.notification_main_channel_id);
|
||||
notificationManager.deleteNotificationChannel(id);
|
||||
} else {
|
||||
notificationManager.cancel(LockService.NOTIFICATION_ID);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
stopService(intent);
|
||||
}
|
||||
|
||||
lockWidgetProvider.setWidgetStop(context);
|
||||
stopService(intent);
|
||||
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||
disabled = false;
|
||||
Toast.makeText(context, "Something went wrong...", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
//------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
return LockService.START_STICKY;
|
||||
}
|
||||
|
@ -126,6 +189,11 @@ public class LockService extends JobIntentService {
|
|||
@Override
|
||||
protected void onHandleWork(@NonNull Intent intent) { }
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private void setSensorListener() {
|
||||
activeListener = new SensorEventListener() {
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.wesaphzt.privatelock.widget;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
|
@ -14,12 +13,6 @@ import android.widget.RemoteViews;
|
|||
|
||||
import com.wesaphzt.privatelock.service.LockService;
|
||||
import com.wesaphzt.privatelock.R;
|
||||
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
||||
|
||||
import static com.wesaphzt.privatelock.service.LockService.CHANNEL_ID;
|
||||
import static com.wesaphzt.privatelock.service.LockService.activeListener;
|
||||
import static com.wesaphzt.privatelock.service.LockService.disabled;
|
||||
import static com.wesaphzt.privatelock.service.LockService.mSensorManager;
|
||||
|
||||
public class LockWidgetProvider extends AppWidgetProvider {
|
||||
|
||||
|
@ -72,33 +65,14 @@ public class LockWidgetProvider extends AppWidgetProvider {
|
|||
editor.putBoolean(context.getString(R.string.widget_prefs_service_id), false);
|
||||
editor.apply();
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
Intent stopIntent = new Intent(context, LockService.class);
|
||||
stopIntent.setAction(LockService.ACTION_STOP_FOREGROUND_SERVICE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
notificationManager.deleteNotificationChannel(CHANNEL_ID);
|
||||
//if countdown timer is running (pause), cancel
|
||||
if(PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
setWidgetStop(context);
|
||||
} else {
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
setWidgetStop(context);
|
||||
}
|
||||
context.startForegroundService(stopIntent);
|
||||
|
||||
} else {
|
||||
notificationManager.cancel(LockService.NOTIFICATION_ID);
|
||||
if(PauseReceiver.isRunning) {
|
||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
setWidgetStop(context);
|
||||
} else {
|
||||
disabled = true;
|
||||
mSensorManager.unregisterListener(activeListener);
|
||||
setWidgetStop(context);
|
||||
}
|
||||
context.startService(stopIntent);
|
||||
}
|
||||
|
||||
//if service is not running
|
||||
|
@ -106,15 +80,13 @@ public class LockWidgetProvider extends AppWidgetProvider {
|
|||
editor.putBoolean(context.getString(R.string.widget_prefs_service_id), true);
|
||||
editor.commit();
|
||||
|
||||
Intent i = new Intent(context, LockService.class);
|
||||
Intent startIntent = new Intent(context, LockService.class);
|
||||
startIntent.setAction(LockService.ACTION_START_FOREGROUND_SERVICE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
disabled = false;
|
||||
context.startForegroundService(i);
|
||||
setWidgetStart(context);
|
||||
context.startForegroundService(startIntent);
|
||||
} else {
|
||||
disabled = false;
|
||||
context.startService(i);
|
||||
setWidgetStart(context);
|
||||
context.startService(startIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue