ShelterService: keep alive for work profile
This commit is contained in:
parent
80bbc633a6
commit
1bf95eafeb
|
@ -1,5 +1,8 @@
|
|||
package net.typeblog.shelter.services;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Intent;
|
||||
|
@ -7,11 +10,13 @@ import android.content.pm.ResolveInfo;
|
|||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import net.typeblog.shelter.R;
|
||||
import net.typeblog.shelter.ShelterApplication;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ShelterService extends Service {
|
||||
private static final String NOTIFICATION_CHANNEL_ID = "ShelterService";
|
||||
private DevicePolicyManager mPolicyManager = null;
|
||||
private boolean mIsWorkProfile = false;
|
||||
private IShelterService.Stub mBinder = new IShelterService.Stub() {
|
||||
|
@ -46,6 +51,12 @@ public class ShelterService extends Service {
|
|||
public void onCreate() {
|
||||
mPolicyManager = getSystemService(DevicePolicyManager.class);
|
||||
mIsWorkProfile = mPolicyManager.isProfileOwnerApp(getPackageName());
|
||||
|
||||
if (mIsWorkProfile) {
|
||||
// In work profile, only this service will be running
|
||||
// so we have to keep it alive
|
||||
setForeground();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@ -53,4 +64,25 @@ public class ShelterService extends Service {
|
|||
public IBinder onBind(Intent intent) {
|
||||
return mBinder;
|
||||
}
|
||||
|
||||
private void setForeground() {
|
||||
// Android O and later: Notification Channel
|
||||
// TODO: Maybe backport to pre-O?
|
||||
NotificationManager nm = getSystemService(NotificationManager.class);
|
||||
if (nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
|
||||
NotificationChannel chan = new NotificationChannel(
|
||||
NOTIFICATION_CHANNEL_ID, getString(R.string.app_name),
|
||||
NotificationManager.IMPORTANCE_DEFAULT);
|
||||
nm.createNotificationChannel(chan);
|
||||
}
|
||||
|
||||
// Create foreground notification to keep the service alive
|
||||
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
|
||||
.setTicker(getString(R.string.app_name))
|
||||
.setContentTitle(getString(R.string.service_title))
|
||||
.setContentText(getString(R.string.service_desc))
|
||||
.setSmallIcon(R.drawable.ic_notification_white_24dp)
|
||||
.build();
|
||||
startForeground(1, notification);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,7 +173,6 @@ public class MainActivity extends AppCompatActivity {
|
|||
finish();
|
||||
}
|
||||
} else if (requestCode == REQUEST_START_SERVICE_IN_WORK_PROFILE && resultCode == RESULT_OK) {
|
||||
// TODO: Set the service in work profile as foreground to keep it alive
|
||||
Bundle extra = data.getBundleExtra("extra");
|
||||
IBinder binder = extra.getBinder("service");
|
||||
mServiceWork = IShelterService.Stub.asInterface(binder);
|
||||
|
|
5
app/src/main/res/drawable/ic_notification_white_24dp.xml
Normal file
5
app/src/main/res/drawable/ic_notification_white_24dp.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24.0" android:viewportWidth="24.0"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#FF000000" android:pathData="M20,13H4c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h16c0.55,0 1,-0.45 1,-1v-6c0,-0.55 -0.45,-1 -1,-1zM7,19c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM20,3H4c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h16c0.55,0 1,-0.45 1,-1V4c0,-0.55 -0.45,-1 -1,-1zM7,9c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2z"/>
|
||||
</vector>
|
|
@ -7,4 +7,6 @@
|
|||
<string name="msg_device_unsupported">Permission is denied or Unsupported device</string>
|
||||
<string name="work_profile_not_found">Work profile not found. Please restart the app to re-provision the profile.</string>
|
||||
<string name="work_profile_provision_failed">Cannot provision work profile. You may try again by restarting Shelter.</string>
|
||||
<string name="service_title">Shelter Service</string>
|
||||
<string name="service_desc">Shelter is now running …</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue