Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.tinker.lib">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application>
<service
android:name=".service.TinkerPatchForeService"
Expand All @@ -18,7 +20,6 @@
<service
android:name=".service.TinkerPatchService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":patch" />
<service
android:name=".service.TinkerPatchService$InnerService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
package com.tencent.tinker.lib.service;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerLoadResult;
import com.tencent.tinker.loader.shareutil.ShareTinkerLog;
Expand Down Expand Up @@ -102,5 +108,28 @@ public boolean checkIfNeedKill(PatchResult result) {
return true;
}

public static class DefaultNotificationProvider {
public static final int NOTIFICATION_ID = 0x1;
private static final String CHANNEL_ID = "tinker_patch";
private static final String CHANNEL_NAME = "Tinker Patch";

public static Notification createNotification(Context context) {
final Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.createNotificationChannel(new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW));
}
builder = new Notification.Builder(context, CHANNEL_ID);
} else {
builder = new Notification.Builder(context);
}
return builder.setContentTitle("Tinker")
.setContentText("Applying patch")
.setOngoing(true)
.setSmallIcon(context.getApplicationInfo().icon)
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.app.ActivityManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -53,6 +55,15 @@ public class TinkerPatchService extends IntentService {
private static AbstractPatch upgradePatchProcessor = null;
private static int notificationId = ShareConstants.TINKER_PATCH_SERVICE_NOTIFICATION;
private static Class<? extends AbstractResultService> resultServiceClass = null;
private static ForegroundNotificationFactory foregroundNotificationFactory = null;

public interface ForegroundNotificationFactory {
Notification create(Service service);
}

public static void setForegroundNotificationFactory(ForegroundNotificationFactory factory) {
foregroundNotificationFactory = factory;
}

public TinkerPatchService() {
super("TinkerPatchService");
Expand All @@ -70,7 +81,11 @@ public static void runPatchService(final Context context, final String path, boo
intent.putExtra(PATCH_USE_EMERGENCY_MODE, useEmergencyMode);
intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
try {
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
} catch (Throwable thr) {
ShareTinkerLog.e(TAG, "run patch service fail, exception:" + thr);
}
Expand Down Expand Up @@ -108,6 +123,47 @@ public static String getPatchResultExtra(Intent intent) {
return ShareIntentUtil.getStringExtra(intent, RESULT_CLASS_EXTRA);
}

@Override
public void onCreate() {
super.onCreate();
ensureForeground();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ensureForeground();
return super.onStartCommand(intent, flags, startId);
}

private void ensureForeground() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
try {
startForeground(notificationId, createForegroundNotification());
} catch (Throwable thr) {
ShareTinkerLog.e(TAG, "startForeground fail, exception:" + thr);
}
}

private Notification createForegroundNotification() {
if (foregroundNotificationFactory != null) {
return foregroundNotificationFactory.create(this);
}
final String channelId = "tinker_patch_service";
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (nm != null && nm.getNotificationChannel(channelId) == null) {
final NotificationChannel channel = new NotificationChannel(channelId, "TinkerPatchService", NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(channel);
}
return new Notification.Builder(this, channelId)
.setSmallIcon(getApplicationInfo().icon)
.setContentTitle("Tinker")
.setContentText("Applying patch")
.setOngoing(true)
.build();
}

@Override
protected void onHandleIntent(Intent intent) {
increasingPriority();
Expand Down