Merge "Move noteAppWidgetTapped call into AppWidgetHostView." into rvc-dev

This commit is contained in:
Hui Yu
2020-04-18 04:03:52 +00:00
committed by Android (Google) Code Review
6 changed files with 45 additions and 48 deletions

View File

@@ -104,7 +104,7 @@ public class AppWidgetHostView extends FrameLayout {
*/ */
public AppWidgetHostView(Context context, OnClickHandler handler) { public AppWidgetHostView(Context context, OnClickHandler handler) {
this(context, android.R.anim.fade_in, android.R.anim.fade_out); this(context, android.R.anim.fade_in, android.R.anim.fade_out);
mOnClickHandler = handler; mOnClickHandler = getHandler(handler);
} }
/** /**
@@ -131,7 +131,7 @@ public class AppWidgetHostView extends FrameLayout {
* @hide * @hide
*/ */
public void setOnClickHandler(OnClickHandler handler) { public void setOnClickHandler(OnClickHandler handler) {
mOnClickHandler = handler; mOnClickHandler = getHandler(handler);
} }
/** /**
@@ -423,7 +423,6 @@ public class AppWidgetHostView extends FrameLayout {
// inflate any requested LayoutParams. // inflate any requested LayoutParams.
mRemoteContext = getRemoteContext(); mRemoteContext = getRemoteContext();
int layoutId = remoteViews.getLayoutId(); int layoutId = remoteViews.getLayoutId();
// If our stale view has been prepared to match active, and the new // If our stale view has been prepared to match active, and the new
// layout matches, try recycling it // layout matches, try recycling it
if (content == null && layoutId == mLayoutId) { if (content == null && layoutId == mLayoutId) {
@@ -711,4 +710,16 @@ public class AppWidgetHostView extends FrameLayout {
} }
return null; return null;
} }
private OnClickHandler getHandler(OnClickHandler handler) {
return (view, pendingIntent, response) -> {
AppWidgetManager.getInstance(mContext).noteAppWidgetTapped(mAppWidgetId);
if (handler != null) {
return handler.onClickHandler(view, pendingIntent, response);
} else {
return RemoteViews.startPendingIntent(view, pendingIntent,
response.getLaunchOptions(view));
}
};
}
} }

View File

@@ -1243,13 +1243,13 @@ public class AppWidgetManager {
/** /**
* Note an app widget is tapped on. * Note an app widget is tapped on.
* @param uid App UID. *
* @param packageName App package name. * @param appWidgetId App widget id.
* @hide * @hide
*/ */
public void noteAppWidgetTapped(int uid, @NonNull String packageName) { public void noteAppWidgetTapped(int appWidgetId) {
try { try {
mService.noteAppWidgetTapped(uid, packageName); mService.noteAppWidgetTapped(mPackageName, appWidgetId);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -29,7 +29,6 @@ import android.app.Application;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.RemoteInput; import android.app.RemoteInput;
import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
@@ -4131,18 +4130,8 @@ public class RemoteViews implements Parcelable, Filter {
// The NEW_TASK flags are applied through the activity options and not as a part of // The NEW_TASK flags are applied through the activity options and not as a part of
// the call to startIntentSender() to ensure that they are consistently applied to // the call to startIntentSender() to ensure that they are consistently applied to
// both mutable and immutable PendingIntents. // both mutable and immutable PendingIntents.
final IntentSender intentSender = pendingIntent.getIntentSender();
final int uid = intentSender.getCreatorUid();
final String packageName = intentSender.getCreatorPackage();
if (uid != -1 && packageName != null) {
final AppWidgetManager appWidgetManager =
context.getSystemService(AppWidgetManager.class);
if (appWidgetManager != null) {
appWidgetManager.noteAppWidgetTapped(uid, packageName);
}
}
context.startIntentSender( context.startIntentSender(
intentSender, options.first, pendingIntent.getIntentSender(), options.first,
0, 0, 0, options.second.toBundle()); 0, 0, 0, options.second.toBundle());
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
Log.e(LOG_TAG, "Cannot send pending intent: ", e); Log.e(LOG_TAG, "Cannot send pending intent: ", e);

View File

@@ -77,6 +77,6 @@ interface IAppWidgetService {
boolean requestPinAppWidget(String packageName, in ComponentName providerComponent, boolean requestPinAppWidget(String packageName, in ComponentName providerComponent,
in Bundle extras, in IntentSender resultIntent); in Bundle extras, in IntentSender resultIntent);
boolean isRequestPinAppWidgetSupported(); boolean isRequestPinAppWidgetSupported();
void noteAppWidgetTapped(int uid, String packageName); oneway void noteAppWidgetTapped(in String callingPackage, in int appWidgetId);
} }

View File

@@ -3652,11 +3652,12 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
* Note an app widget is tapped on. If a app widget is tapped, the underlying app is treated as * Note an app widget is tapped on. If a app widget is tapped, the underlying app is treated as
* foreground so the app can get while-in-use permission. * foreground so the app can get while-in-use permission.
* *
* @param uid UID of the underlying app. * @param callingPackage calling app's packageName.
* @param packageName Package name of the app. * @param appWidgetId App widget id.
*/ */
@Override @Override
public void noteAppWidgetTapped(int uid, String packageName) { public void noteAppWidgetTapped(String callingPackage, int appWidgetId) {
mSecurityPolicy.enforceCallFromPackage(callingPackage);
final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity();
try { try {
@@ -3665,32 +3666,22 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
if (procState > ActivityManager.PROCESS_STATE_TOP) { if (procState > ActivityManager.PROCESS_STATE_TOP) {
return; return;
} }
synchronized (mLock) {
// Default launcher from package manager. final Widget widget = lookupWidgetLocked(appWidgetId, callingUid, callingPackage);
final ComponentName defaultLauncher = mPackageManagerInternal if (widget == null) {
.getDefaultHomeActivity(UserHandle.getUserId(callingUid)); return;
if (defaultLauncher == null) { }
return; final ProviderId providerId = widget.provider.id;
final String packageName = providerId.componentName.getPackageName();
if (packageName == null) {
return;
}
final SparseArray<String> uid2PackageName = new SparseArray<String>();
uid2PackageName.put(providerId.uid, packageName);
mAppOpsManagerInternal.updateAppWidgetVisibility(uid2PackageName, true);
mUsageStatsManagerInternal.reportEvent(packageName,
UserHandle.getUserId(providerId.uid), UsageEvents.Event.USER_INTERACTION);
} }
int defaultLauncherUid = 0;
try {
defaultLauncherUid = mPackageManager.getApplicationInfo(
defaultLauncher.getPackageName(), 0 ,
UserHandle.getUserId(callingUid)).uid;
} catch (RemoteException e) {
Slog.e(TAG, "Failed to getApplicationInfo for package:"
+ defaultLauncher.getPackageName(), e);
return;
}
// The callingUid must be default launcher uid.
if (defaultLauncherUid != callingUid) {
return;
}
final SparseArray<String> uid2PackageName = new SparseArray<String>();
uid2PackageName.put(uid, packageName);
mAppOpsManagerInternal.updateAppWidgetVisibility(uid2PackageName, true);
mUsageStatsManagerInternal.reportEvent(packageName, UserHandle.getUserId(uid),
UsageEvents.Event.USER_INTERACTION);
} finally { } finally {
Binder.restoreCallingIdentity(ident); Binder.restoreCallingIdentity(ident);
} }

View File

@@ -5308,6 +5308,12 @@ public class AppOpsService extends IAppOpsService.Stub {
pw.print(" pendingCapability="); pw.print(" pendingCapability=");
pw.println(uidState.pendingCapability); pw.println(uidState.pendingCapability);
} }
pw.print(" appWidgetVisible=");
pw.println(uidState.appWidgetVisible);
if (uidState.appWidgetVisible != uidState.pendingAppWidgetVisible) {
pw.print(" pendingAppWidgetVisible=");
pw.println(uidState.pendingAppWidgetVisible);
}
if (uidState.pendingStateCommitTime != 0) { if (uidState.pendingStateCommitTime != 0) {
pw.print(" pendingStateCommitTime="); pw.print(" pendingStateCommitTime=");
TimeUtils.formatDuration(uidState.pendingStateCommitTime, nowElapsed, pw); TimeUtils.formatDuration(uidState.pendingStateCommitTime, nowElapsed, pw);