Commit 2bcdf993 authored by Румен Петров's avatar Румен Петров
Browse files

manage service from activity

parent 1edcffcf
......@@ -17,7 +17,6 @@
package com.termoneplus.securebox;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
......@@ -49,7 +48,6 @@ public class Application extends android.app.Application {
public static File scriptdir; /*for backward compatibility*/
private boolean installed = false;
private Intent paths_intent;
public static boolean isAppInstalled(Context context, String packageName) {
try {
......@@ -87,15 +85,6 @@ public class Application extends android.app.Application {
installed = onInstall();
new Handler(Looper.getMainLooper()).post(this::onInstallStatus);
}).start();
paths_intent = new Intent(this, MainService.class);
startService(paths_intent);
}
@Override
public void onTerminate() {
stopService(paths_intent);
super.onTerminate();
}
private void onInstallStatus() {
......
......@@ -58,12 +58,17 @@ public class MainActivity extends AppCompatActivity {
private Button install_terminal;
private Button open_terminal;
// Flag to stop foreground service if requested from menu.
private boolean force_service_exit = false;
private Intent main_service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_service = MainService.start(this);
main_layout = findViewById(R.id.main_layout);
{
......@@ -112,6 +117,15 @@ public class MainActivity extends AppCompatActivity {
super.onPause();
}
@Override
protected void onDestroy() {
if (force_service_exit) {
stopService(main_service);
}
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
......@@ -141,6 +155,11 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
return true;
}
if (id == R.id.action_exit) {
force_service_exit = true;
finishAffinity();
return true;
}
return super.onOptionsItemSelected(item);
}
......
......@@ -21,6 +21,7 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
......@@ -39,6 +40,15 @@ public class MainService extends Service {
private final IBinder binder = new Binder();
private PathResolver path_resolver;
public static Intent start(Context context) {
Intent intent = new Intent(context, MainService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O /*API Level 26*/)
context.startForegroundService(intent);
else
context.startService(intent);
return intent;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
......
......@@ -39,6 +39,7 @@ public class PathBroadcast extends BroadcastReceiver {
switch (action) {
case ACTION_PATH_PREPEND_BROADCAST: {
if (Application.scriptdir == null) break;
MainService.start(context);
/* The directory we want prepended goes into the result extras */
Bundle result = getResultExtras(true);
result.putString(packageName, Application.scriptdir.getAbsolutePath());
......@@ -46,6 +47,7 @@ public class PathBroadcast extends BroadcastReceiver {
break;
}
case ACTION_PATH_APPEND_BROADCAST: {
MainService.start(context);
/* The directory we want appended goes into the result extras */
Bundle result = getResultExtras(true);
result.putString(packageName, Application.xbindir.getPath());
......
......@@ -26,6 +26,12 @@
android:title="@string/action_account"
app:showAsAction="never"/>
<item
android:id="@+id/action_exit"
android:orderInCategory="100"
android:title="@string/action_exit"
app:showAsAction="never"/>
<item
android:id="@+id/action_help"
android:orderInCategory="100"
......
......@@ -39,6 +39,7 @@
<string name="open_terminal">Open Terminal</string>
<!-- menu actions -->
<string name="action_account">Virtual Account</string>
<string name="action_exit">Force Exit</string>
<string name="action_help">Help</string>
<string name="action_about">About</string>
<!-- service "Main" -->
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment