Skip to content

Commit

Permalink
Run Service with Notification && Cold & Warm boot
Browse files Browse the repository at this point in the history
Suits Android 8.0 also
Related to issue #44
  • Loading branch information
renyuneyun committed Oct 7, 2018
1 parent 75fda93 commit 1ff3338
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@
android:label="@string/title_setting" />

<receiver
android:name=".core.BootupReceiver"
android:enabled="false"
android:name=".core.BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- Added for issue #44, to test if related to warm boot and cold boot {{{ -->
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<!-- }}} -->
</intent-filter>
</receiver>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
import android.content.Context;
import android.content.Intent;

public class BootupReceiver extends BroadcastReceiver {
import com.orhanobut.logger.Logger;

public class BootUpReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
Logger.d("BootUp onReceive <%s>", intent);
String action = intent.getAction();
if (Intent.ACTION_BOOT_COMPLETED.equals(action)
|| "android.intent.action.QUICKBOOT_POWERON".equals(action))
EHService.start(context);
}
}
27 changes: 26 additions & 1 deletion app/src/main/java/ryey/easer/core/EHService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package ryey.easer.core;

import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
Expand All @@ -28,9 +29,11 @@
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;

import com.orhanobut.logger.Logger;

Expand All @@ -39,6 +42,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import ryey.easer.R;
import ryey.easer.core.data.ScriptTree;
import ryey.easer.core.data.storage.ScriptDataStorage;
import ryey.easer.core.log.ActivityLogService;
Expand Down Expand Up @@ -79,6 +83,7 @@ public static void unregisterConditionEventNotifier(@NonNull Context context, @N

private static final String TAG = "[EHService] ";
private static final String SERVICE_NAME = "Easer";
private static final int NOTIFICATION_ID = 1;

List<Lotus> mLotusArray = new ArrayList<>();
private ExecutorService executorService = Executors.newFixedThreadPool(4);
Expand Down Expand Up @@ -129,7 +134,11 @@ public static boolean isRunning() {

public static void start(Context context) {
Intent intent = new Intent(context, EHService.class);
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}

public static void stop(Context context) {
Expand All @@ -143,10 +152,26 @@ public static void reload(Context context) {
context.sendBroadcast(intent);
}

private static Notification getIndicatorNotification(Context context) {
Notification indicatorNotification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.easer))
.setContentText(context.getString(
R.string.text_notification_running_indicator_content,
context.getString(R.string.easer)))
.build();
return indicatorNotification;
}

private void runInForeground() {
Notification indicatorNotification = getIndicatorNotification(this);
startForeground(NOTIFICATION_ID, indicatorNotification);
}

@Override
public void onCreate() {
Logger.v(TAG + "onCreate()");
super.onCreate();
runInForeground();
ActivityLogService.Companion.notifyServiceStatus(this, SERVICE_NAME, true, null);
bindService(new Intent(this, ConditionHolderService.class), connection, Context.BIND_AUTO_CREATE);
running = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import ryey.easer.BuildConfig;
import ryey.easer.R;
import ryey.easer.Utils;
import ryey.easer.core.BootupReceiver;
import ryey.easer.core.BootUpReceiver;
import ryey.easer.core.EHService;
import ryey.easer.core.UpgradeCompleteReceiver;
import ryey.easer.core.data.Helper;
Expand Down Expand Up @@ -84,7 +84,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.key_pref_autostart))) {
ComponentName componentName = new ComponentName(this, BootupReceiver.class);
ComponentName componentName = new ComponentName(this, BootUpReceiver.class);
PackageManager pm = getPackageManager();
if (sharedPreferences.getBoolean(key, false)) {
pm.setComponentEnabledSetting(componentName,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@
<string name="category_operation_easer">@string/easer</string>
<string name="category_operation_misc">雜項</string>
<string name="category_operation_android">Android</string>
<string name="text_notification_running_indicator_content">%s正在後臺運行</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="text_lack_permission">Easer does NOT have all permissions it needs.</string>
<string name="button_request_permission">Grant permissions</string>

<string name="text_notification_running_indicator_content">%s is running in the background</string>

<string name="text_title">Title</string>
<string name="text_parent_node">Parent</string>
<string name="text_parent">Parent</string>
Expand Down

0 comments on commit 1ff3338

Please sign in to comment.