Merge "[Telephony MainLine] Add PackageChangeReceiver to replace PackageMonitor"

This commit is contained in:
Betty Chang
2019-12-10 02:14:10 +00:00
committed by Gerrit Code Review
2 changed files with 172 additions and 5 deletions

View File

@@ -0,0 +1,167 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.telephony;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import com.android.internal.os.BackgroundThread;
/**
* Helper class for monitoring the state of packages: adding, removing,
* updating, and disappearing and reappearing on the SD card.
*/
public abstract class PackageChangeReceiver extends BroadcastReceiver {
static final IntentFilter sPackageIntentFilter = new IntentFilter();
static {
sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
sPackageIntentFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
sPackageIntentFilter.addDataScheme("package");
}
Context mRegisteredContext;
/**
* To register the intents that needed for monitoring the state of packages
*/
public void register(@NonNull Context context, @Nullable Looper thread,
@Nullable UserHandle user) {
if (mRegisteredContext != null) {
throw new IllegalStateException("Already registered");
}
Handler handler = (thread == null) ? BackgroundThread.getHandler() : new Handler(thread);
mRegisteredContext = context;
if (handler != null) {
if (user != null) {
context.registerReceiverAsUser(this, user, sPackageIntentFilter, null, handler);
} else {
context.registerReceiver(this, sPackageIntentFilter,
null, handler);
}
} else {
throw new NullPointerException();
}
}
/**
* To unregister the intents for monitoring the state of packages
*/
public void unregister() {
if (mRegisteredContext == null) {
throw new IllegalStateException("Not registered");
}
mRegisteredContext.unregisterReceiver(this);
mRegisteredContext = null;
}
/**
* This method is invoked when receive the Intent.ACTION_PACKAGE_ADDED
*/
public void onPackageAdded(@Nullable String packageName) {
}
/**
* This method is invoked when receive the Intent.ACTION_PACKAGE_REMOVED
*/
public void onPackageRemoved(@Nullable String packageName) {
}
/**
* This method is invoked when Intent.EXTRA_REPLACING as extra field is true
*/
public void onPackageUpdateFinished(@Nullable String packageName) {
}
/**
* This method is invoked when receive the Intent.ACTION_PACKAGE_CHANGED or
* Intent.EXTRA_REPLACING as extra field is true
*/
public void onPackageModified(@Nullable String packageName) {
}
/**
* This method is invoked when receive the Intent.ACTION_QUERY_PACKAGE_RESTART and
* Intent.ACTION_PACKAGE_RESTARTED
*/
public void onHandleForceStop(@Nullable String[] packages, boolean doit) {
}
/**
* This method is invoked when receive the Intent.ACTION_PACKAGE_REMOVED
*/
public void onPackageDisappeared() {
}
/**
* This method is invoked when receive the Intent.ACTION_PACKAGE_ADDED
*/
public void onPackageAppeared() {
}
@Override
public void onReceive(@Nullable Context context, @Nullable Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
String pkg = getPackageName(intent);
if (pkg != null) {
if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
onPackageUpdateFinished(pkg);
onPackageModified(pkg);
} else {
onPackageAdded(pkg);
}
onPackageAppeared();
}
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
String pkg = getPackageName(intent);
if (pkg != null) {
if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
onPackageRemoved(pkg);
}
onPackageDisappeared();
}
} else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
String pkg = getPackageName(intent);
if (pkg != null) {
onPackageModified(pkg);
}
} else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
String[] disappearingPackages = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
onHandleForceStop(disappearingPackages, false);
} else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
String[] disappearingPackages = new String[] {getPackageName(intent)};
onHandleForceStop(disappearingPackages, true);
}
}
String getPackageName(Intent intent) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
return pkg;
}
}

View File

@@ -39,11 +39,11 @@ import android.os.Process;
import android.os.UserHandle;
import android.provider.Telephony;
import android.provider.Telephony.Sms.Intents;
import android.telephony.PackageChangeReceiver;
import android.telephony.Rlog;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.content.PackageMonitor;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -782,7 +782,7 @@ public final class SmsApplication {
* Tracks package changes and ensures that the default SMS app is always configured to be the
* preferred activity for SENDTO sms/mms intents.
*/
private static final class SmsPackageMonitor extends PackageMonitor {
private static final class SmsPackageMonitor extends PackageChangeReceiver {
final Context mContext;
public SmsPackageMonitor(Context context) {
@@ -791,12 +791,12 @@ public final class SmsApplication {
}
@Override
public void onPackageDisappeared(String packageName, int reason) {
public void onPackageDisappeared() {
onPackageChanged();
}
@Override
public void onPackageAppeared(String packageName, int reason) {
public void onPackageAppeared() {
onPackageChanged();
}
@@ -829,7 +829,7 @@ public final class SmsApplication {
public static void initSmsPackageMonitor(Context context) {
sSmsPackageMonitor = new SmsPackageMonitor(context);
sSmsPackageMonitor.register(context, context.getMainLooper(), UserHandle.ALL, false);
sSmsPackageMonitor.register(context, context.getMainLooper(), UserHandle.ALL);
}
@UnsupportedAppUsage