mirror of
https://github.com/wesaphzt/privatelock.git
synced 2025-04-19 14:09: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.FragmentDonate;
|
||||||
import com.wesaphzt.privatelock.fragments.FragmentSettings;
|
import com.wesaphzt.privatelock.fragments.FragmentSettings;
|
||||||
import com.wesaphzt.privatelock.receivers.DeviceAdminReceiver;
|
import com.wesaphzt.privatelock.receivers.DeviceAdminReceiver;
|
||||||
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
|
||||||
import com.wesaphzt.privatelock.service.LockService;
|
import com.wesaphzt.privatelock.service.LockService;
|
||||||
import com.wesaphzt.privatelock.widget.LockWidgetProvider;
|
import com.wesaphzt.privatelock.widget.LockWidgetProvider;
|
||||||
|
|
||||||
|
@ -291,27 +290,12 @@ public class MainActivity extends AppCompatActivity {
|
||||||
private void startServicePrep() {
|
private void startServicePrep() {
|
||||||
//stop this listener
|
//stop this listener
|
||||||
mSensorManager.unregisterListener(mActiveListener);
|
mSensorManager.unregisterListener(mActiveListener);
|
||||||
Intent intent = new Intent(context, LockService.class);
|
|
||||||
|
|
||||||
try {
|
//start service intent
|
||||||
//stop any already running service
|
Intent startIntent = new Intent(context, LockService.class);
|
||||||
LockService.mSensorManager.unregisterListener(LockService.activeListener);
|
startIntent.setAction(LockService.ACTION_START_FOREGROUND_SERVICE);
|
||||||
startLockService(intent);
|
|
||||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
|
||||||
lockWidgetProvider.setWidgetStart(context);
|
|
||||||
|
|
||||||
//cancel any pause timer that might be running
|
startLockService(startIntent);
|
||||||
try {
|
|
||||||
PauseReceiver.mCountdown.cancel();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
startLockService(intent);
|
|
||||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
|
||||||
lockWidgetProvider.setWidgetStart(context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,20 +1,11 @@
|
||||||
package com.wesaphzt.privatelock.receivers;
|
package com.wesaphzt.privatelock.receivers;
|
||||||
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
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.SharedPreferences;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
|
|
||||||
import com.wesaphzt.privatelock.service.LockService;
|
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 {
|
public class NotificationReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@ -23,37 +14,13 @@ public class NotificationReceiver extends BroadcastReceiver {
|
||||||
String action = intent.getStringExtra("lock_service");
|
String action = intent.getStringExtra("lock_service");
|
||||||
|
|
||||||
if (action.equals("lock_service_notification")) {
|
if (action.equals("lock_service_notification")) {
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
Intent stopIntent = new Intent(context, LockService.class);
|
||||||
NotificationManager notificationManager =
|
stopIntent.setAction(LockService.ACTION_STOP_FOREGROUND_SERVICE);
|
||||||
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
String id = context.getString(R.string.notification_main_channel_id);
|
context.startForegroundService(stopIntent);
|
||||||
notificationManager.deleteNotificationChannel(id);
|
|
||||||
if(PauseReceiver.isRunning) {
|
|
||||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
|
||||||
disabled = true;
|
|
||||||
mSensorManager.unregisterListener(activeListener);
|
|
||||||
lockWidgetProvider.setWidgetStop(context);
|
|
||||||
} else {
|
} else {
|
||||||
disabled = true;
|
context.startService(stopIntent);
|
||||||
mSensorManager.unregisterListener(activeListener);
|
|
||||||
lockWidgetProvider.setWidgetStop(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import com.wesaphzt.privatelock.receivers.DeviceAdminReceiver;
|
||||||
import com.wesaphzt.privatelock.receivers.NotificationReceiver;
|
import com.wesaphzt.privatelock.receivers.NotificationReceiver;
|
||||||
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
import com.wesaphzt.privatelock.receivers.PauseReceiver;
|
||||||
import com.wesaphzt.privatelock.receivers.PresenceReceiver;
|
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_LOW;
|
||||||
import static androidx.core.app.NotificationCompat.PRIORITY_MIN;
|
import static androidx.core.app.NotificationCompat.PRIORITY_MIN;
|
||||||
|
@ -67,24 +68,37 @@ public class LockService extends JobIntentService {
|
||||||
public static final int DEFAULT_SENSITIVITY = 10;
|
public static final int DEFAULT_SENSITIVITY = 10;
|
||||||
public static int SENSITIVITY;
|
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
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
context = getApplicationContext();
|
context = getApplicationContext();
|
||||||
CHANNEL_ID = getString(R.string.notification_main_channel_id);
|
CHANNEL_ID = getString(R.string.notification_main_channel_id);
|
||||||
CHANNEL_NAME = getString(R.string.notification_main_channel_name);
|
CHANNEL_NAME = getString(R.string.notification_main_channel_name);
|
||||||
//------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------
|
||||||
PresenceReceiver presenceReceiver = new PresenceReceiver();
|
if (intent != null) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
|
||||||
|
LockWidgetProvider lockWidgetProvider = new LockWidgetProvider();
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
|
||||||
|
case ACTION_START_FOREGROUND_SERVICE:
|
||||||
|
presenceReceiver = new PresenceReceiver();
|
||||||
|
|
||||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_PRESENT);
|
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_PRESENT);
|
||||||
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||||
registerReceiver(presenceReceiver, intentFilter);
|
registerReceiver(presenceReceiver, intentFilter);
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
|
||||||
try {
|
try {
|
||||||
SENSITIVITY = prefs.getInt(MainActivity.PREFS_THRESHOLD, DEFAULT_SENSITIVITY);
|
SENSITIVITY = prefs.getInt(MainActivity.PREFS_THRESHOLD, DEFAULT_SENSITIVITY);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Toast.makeText(context, "Unable to retrieve threshold", Toast.LENGTH_LONG).show();
|
Toast.makeText(context, "Unable to retrieve threshold", Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------
|
||||||
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
@ -107,13 +121,62 @@ public class LockService extends JobIntentService {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
//create foreground service
|
//create foreground service
|
||||||
startForeground(NOTIFICATION_ID, notification);
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
lockWidgetProvider.setWidgetStart(context);
|
||||||
disabled = false;
|
disabled = false;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
notificationManager.notify(NOTIFICATION_ID, notification);
|
notificationManager.notify(NOTIFICATION_ID, notification);
|
||||||
|
lockWidgetProvider.setWidgetStart(context);
|
||||||
disabled = false;
|
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 {
|
||||||
|
Toast.makeText(context, "Something went wrong...", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return LockService.START_STICKY;
|
return LockService.START_STICKY;
|
||||||
}
|
}
|
||||||
|
@ -126,6 +189,11 @@ public class LockService extends JobIntentService {
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleWork(@NonNull Intent intent) { }
|
protected void onHandleWork(@NonNull Intent intent) { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
private void setSensorListener() {
|
private void setSensorListener() {
|
||||||
activeListener = new SensorEventListener() {
|
activeListener = new SensorEventListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.wesaphzt.privatelock.widget;
|
package com.wesaphzt.privatelock.widget;
|
||||||
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.appwidget.AppWidgetManager;
|
import android.appwidget.AppWidgetManager;
|
||||||
import android.appwidget.AppWidgetProvider;
|
import android.appwidget.AppWidgetProvider;
|
||||||
|
@ -14,12 +13,6 @@ import android.widget.RemoteViews;
|
||||||
|
|
||||||
import com.wesaphzt.privatelock.service.LockService;
|
import com.wesaphzt.privatelock.service.LockService;
|
||||||
import com.wesaphzt.privatelock.R;
|
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 {
|
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.putBoolean(context.getString(R.string.widget_prefs_service_id), false);
|
||||||
editor.apply();
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
notificationManager.deleteNotificationChannel(CHANNEL_ID);
|
context.startForegroundService(stopIntent);
|
||||||
//if countdown timer is running (pause), cancel
|
|
||||||
if(PauseReceiver.isRunning) {
|
|
||||||
PauseReceiver.mCountdown.cancel(); PauseReceiver.isRunning = false;
|
|
||||||
disabled = true;
|
|
||||||
mSensorManager.unregisterListener(activeListener);
|
|
||||||
setWidgetStop(context);
|
|
||||||
} else {
|
} else {
|
||||||
disabled = true;
|
context.startService(stopIntent);
|
||||||
mSensorManager.unregisterListener(activeListener);
|
|
||||||
setWidgetStop(context);
|
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//if service is not running
|
//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.putBoolean(context.getString(R.string.widget_prefs_service_id), true);
|
||||||
editor.commit();
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
disabled = false;
|
context.startForegroundService(startIntent);
|
||||||
context.startForegroundService(i);
|
|
||||||
setWidgetStart(context);
|
|
||||||
} else {
|
} else {
|
||||||
disabled = false;
|
context.startService(startIntent);
|
||||||
context.startService(i);
|
|
||||||
setWidgetStart(context);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue