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
17package com.android.tv.settings.device.apps;
18
19import android.app.Activity;
20import android.app.ActivityManager;
21import android.app.admin.DevicePolicyManager;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.ApplicationInfo;
26import android.net.Uri;
27import android.os.Bundle;
28import android.os.UserHandle;
29import android.support.annotation.NonNull;
30import android.support.v17.leanback.widget.GuidanceStylist;
31
32import com.android.settingslib.applications.ApplicationsState;
33import com.android.tv.settings.R;
34
35public class ForceStopPreference extends AppActionPreference {
36
37    public ForceStopPreference(Context context, ApplicationsState.AppEntry entry) {
38        super(context, entry);
39        ConfirmationFragment.prepareArgs(getExtras(), mEntry.info.packageName);
40        refresh();
41    }
42
43    public void refresh() {
44        setTitle(R.string.device_apps_app_management_force_stop);
45        DevicePolicyManager dpm = (DevicePolicyManager) getContext().getSystemService(
46                Context.DEVICE_POLICY_SERVICE);
47        if (dpm.packageHasActiveAdmins(mEntry.info.packageName)) {
48            // User can't force stop device admin.
49            setVisible(false);
50        } else if ((mEntry.info.flags & ApplicationInfo.FLAG_STOPPED) == 0) {
51            // If the app isn't explicitly stopped, then always show the
52            // force stop action.
53            setVisible(true);
54        } else {
55            Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
56                    Uri.fromParts("package", mEntry.info.packageName, null));
57            intent.putExtra(Intent.EXTRA_PACKAGES, new String[] {
58                    mEntry.info.packageName });
59            intent.putExtra(Intent.EXTRA_UID, mEntry.info.uid);
60            intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mEntry.info.uid));
61            getContext().sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
62                @Override
63                public void onReceive(Context context, Intent intent) {
64                    setVisible(getResultCode() != Activity.RESULT_CANCELED);
65                }
66            }, null, Activity.RESULT_CANCELED, null, null);
67        }
68    }
69
70    @Override
71    public String getFragment() {
72        return ConfirmationFragment.class.getName();
73    }
74
75    public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment {
76        private static final String ARG_PACKAGE_NAME = "packageName";
77
78        private static void prepareArgs(@NonNull Bundle args, String packageName) {
79            args.putString(ARG_PACKAGE_NAME, packageName);
80        }
81
82        @NonNull
83        @Override
84        public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
85            final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment();
86            return new GuidanceStylist.Guidance(
87                    getString(R.string.device_apps_app_management_force_stop),
88                    getString(R.string.device_apps_app_management_force_stop_desc),
89                    fragment.getAppName(),
90                    fragment.getAppIcon());
91        }
92
93        @Override
94        public void onOk() {
95            ActivityManager am = (ActivityManager)
96                    getActivity().getSystemService(Context.ACTIVITY_SERVICE);
97            am.forceStopPackage(getArguments().getString(ARG_PACKAGE_NAME));
98        }
99    }
100}
101