165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/* 265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project 365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License"); 565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License. 665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at 765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * http://www.apache.org/licenses/LICENSE-2.0 965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software 1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS, 1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and 1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License. 1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.device.apps; 1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Activity; 2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.ActivityManager; 2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.admin.DevicePolicyManager; 2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.BroadcastReceiver; 2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context; 2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent; 2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.ApplicationInfo; 2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.Uri; 2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserHandle; 2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/** 3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Handles force stopping an application. 3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneclass ForceStopManager { 3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final Context mContext; 3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private AppInfo mAppInfo; 3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private boolean mShowForceStop; 3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ForceStopManager(Context context, AppInfo appInfo) { 3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mContext = context; 4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mAppInfo = appInfo; 4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mShowForceStop = false; 4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane boolean canForceStop() { 4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane checkForceStop(); 4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return mShowForceStop; 4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane void forceStop(ApplicationsState state) { 5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); 5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane am.forceStopPackage(mAppInfo.getPackageName()); 5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane state.invalidatePackage(mAppInfo.getPackageName()); 5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ApplicationsState.AppEntry newEnt = state.getEntry(mAppInfo.getPackageName()); 5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (newEnt != null) { 5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mAppInfo.setEntry(newEnt); 5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private void checkForceStop() { 6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( 6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Context.DEVICE_POLICY_SERVICE); 6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (dpm.packageHasActiveAdmins(mAppInfo.getPackageName())) { 6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // User can't force stop device admin. 6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mShowForceStop = false; 6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else if (!mAppInfo.isStopped()) { 6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // If the app isn't explicitly stopped, then always show the 6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // force stop action. 6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mShowForceStop = true; 6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART, 7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Uri.fromParts("package", mAppInfo.getPackageName(), null)); 7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { 7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mAppInfo.getPackageName() }); 7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane intent.putExtra(Intent.EXTRA_UID, mAppInfo.getUid()); 7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mAppInfo.getUid())); 7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { 7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void onReceive(Context context, Intent intent) { 7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mShowForceStop = (getResultCode() != Activity.RESULT_CANCELED); 8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane }, null, Activity.RESULT_CANCELED, null, null); 8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane} 85