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.content.Context;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.net.Uri;
23import android.os.Bundle;
24import android.support.annotation.NonNull;
25import android.support.v17.leanback.widget.GuidanceStylist;
26
27import com.android.settingslib.applications.ApplicationsState;
28import com.android.tv.settings.R;
29
30public class UninstallPreference extends AppActionPreference {
31
32    public UninstallPreference(Context context,
33            ApplicationsState.AppEntry entry) {
34        super(context, entry);
35        refresh();
36    }
37
38    public void refresh() {
39        if (canUninstall()) {
40            setVisible(true);
41            setTitle(R.string.device_apps_app_management_uninstall);
42        } else if (canUninstallUpdates()) {
43            setVisible(true);
44            setTitle(R.string.device_apps_app_management_uninstall_updates);
45        } else {
46            setVisible(false);
47        }
48    }
49
50    public boolean canUninstall() {
51        return canUninstall(mEntry);
52    }
53
54    public static boolean canUninstall(ApplicationsState.AppEntry entry) {
55        return (entry.info.flags &
56                (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP|ApplicationInfo.FLAG_SYSTEM)) == 0;
57    }
58
59    public boolean canUninstallUpdates() {
60        return (mEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
61    }
62
63    @Override
64    public Intent getIntent() {
65        final Uri packageURI = Uri.parse("package:" + mEntry.info.packageName);
66        final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
67        uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, true);
68        uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
69        return uninstallIntent;
70    }
71}
72