1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.tv.settings.device.apps; 18 19 import android.app.Activity; 20 import android.app.ActivityManager; 21 import android.app.admin.DevicePolicyManager; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.ApplicationInfo; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.UserHandle; 29 import android.support.annotation.NonNull; 30 import android.support.v17.leanback.widget.GuidanceStylist; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settingslib.applications.ApplicationsState; 34 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 35 import com.android.tv.settings.R; 36 37 public class ForceStopPreference extends AppActionPreference { 38 39 public ForceStopPreference(Context context, ApplicationsState.AppEntry entry) { 40 super(context, entry); 41 ConfirmationFragment.prepareArgs(getExtras(), mEntry.info.packageName); 42 refresh(); 43 } 44 45 public void refresh() { 46 setTitle(R.string.device_apps_app_management_force_stop); 47 DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService( 48 Context.DEVICE_POLICY_SERVICE); 49 if (dpm.packageHasActiveAdmins(mEntry.info.packageName)) { 50 // User can't force stop device admin. 51 setVisible(false); 52 } else if ((mEntry.info.flags & ApplicationInfo.FLAG_STOPPED) == 0) { 53 // If the app isn't explicitly stopped, then always show the 54 // force stop action. 55 setVisible(true); 56 } else { 57 Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART, 58 Uri.fromParts("package", mEntry.info.packageName, null)); 59 intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { 60 mEntry.info.packageName }); 61 intent.putExtra(Intent.EXTRA_UID, mEntry.info.uid); 62 intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mEntry.info.uid)); 63 getContext().sendOrderedBroadcast(intent, null, new BroadcastReceiver() { 64 @Override 65 public void onReceive(Context context, Intent intent) { 66 setVisible(getResultCode() != Activity.RESULT_CANCELED); 67 } 68 }, null, Activity.RESULT_CANCELED, null, null); 69 } 70 } 71 72 @Override 73 public String getFragment() { 74 return ConfirmationFragment.class.getName(); 75 } 76 77 public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment { 78 private static final String ARG_PACKAGE_NAME = "packageName"; 79 80 private final MetricsFeatureProvider mMetricsFeatureProvider = 81 new MetricsFeatureProvider(); 82 83 private static void prepareArgs(@NonNull Bundle args, String packageName) { 84 args.putString(ARG_PACKAGE_NAME, packageName); 85 } 86 87 @NonNull 88 @Override 89 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 90 final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment(); 91 return new GuidanceStylist.Guidance( 92 getString(R.string.device_apps_app_management_force_stop), 93 getString(R.string.device_apps_app_management_force_stop_desc), 94 fragment.getAppName(), 95 fragment.getAppIcon()); 96 } 97 98 @Override 99 public void onOk() { 100 String pkgName = getArguments().getString(ARG_PACKAGE_NAME); 101 mMetricsFeatureProvider.action(getContext(), MetricsEvent.ACTION_APP_FORCE_STOP, 102 pkgName); 103 ActivityManager am = (ActivityManager) 104 getActivity().getSystemService(Context.ACTIVITY_SERVICE); 105 am.forceStopPackage(pkgName); 106 } 107 } 108 } 109