• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.car.settings.applications;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ResolveInfo;
23 import android.content.res.Resources;
24 import android.icu.text.ListFormatter;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.AnimationUtil;
30 import com.android.car.settings.common.TextLineItem;
31 import com.android.settingslib.Utils;
32 import com.android.settingslib.applications.PermissionsSummaryHelper;
33 import com.android.settingslib.applications.PermissionsSummaryHelper.PermissionsResultCallback;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 /**
39  * Shows details about an application and action associated with that application,
40  * like uninstall, forceStop.
41  */
42 public class ApplicationPermissionLineItem extends TextLineItem {
43     private static final String TAG = "AppPermissionLineItem";
44 
45     private final ResolveInfo mResolveInfo;
46     private final Context mContext;
47     private TextLineItem.ViewHolder mViewHolder;
48     private CharSequence mSummary;
49 
ApplicationPermissionLineItem(Context context, ResolveInfo resolveInfo)50     public ApplicationPermissionLineItem(Context context, ResolveInfo resolveInfo) {
51         super(context.getText(R.string.permissions_label));
52         mResolveInfo = resolveInfo;
53         mContext = context;
54 
55         PermissionsSummaryHelper.getPermissionSummary(mContext,
56                 mResolveInfo.activityInfo.packageName, mPermissionCallback);
57     }
58 
59     @Override
bindViewHolder(TextLineItem.ViewHolder viewHolder)60     public void bindViewHolder(TextLineItem.ViewHolder viewHolder) {
61         mViewHolder = viewHolder;
62         viewHolder.titleView.setText(mTitle);
63         if (TextUtils.isEmpty(mSummary)) {
64             viewHolder.itemView.setOnClickListener(null);
65             viewHolder.descView.setText(R.string.computing_size);
66             viewHolder.itemView.setEnabled(false);
67         } else {
68             viewHolder.itemView.setOnClickListener(mOnClickListener);
69             viewHolder.descView.setText(mSummary);
70             viewHolder.itemView.setEnabled(true);
71         }
72     }
73 
74     @Override
isEnabled()75     public boolean isEnabled() {
76         return true;
77     }
78 
79     @Override
isExpandable()80     public boolean isExpandable() {
81         return true;
82     }
83 
84     @Override
getDesc()85     public CharSequence getDesc() {
86         return null;
87     }
88 
89     @Override
onClick()90     public void onClick() {
91         // start new activity to manage app permissions
92         Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
93         intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mResolveInfo.activityInfo.packageName);
94         try {
95             mContext.startActivity(
96                     intent, AnimationUtil.slideInFromRightOption(mContext).toBundle());
97         } catch (ActivityNotFoundException e) {
98             Log.w(TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
99         }
100     }
101 
102     private final PermissionsResultCallback mPermissionCallback = new PermissionsResultCallback() {
103         @Override
104         public void onPermissionSummaryResult(int standardGrantedPermissionCount,
105                 int requestedPermissionCount, int additionalGrantedPermissionCount,
106                 List<CharSequence> grantedGroupLabels) {
107             Resources res = mContext.getResources();
108 
109             if (requestedPermissionCount == 0) {
110                 mSummary = res.getString(
111                         R.string.runtime_permissions_summary_no_permissions_requested);
112             } else {
113                 ArrayList<CharSequence> list = new ArrayList<>(grantedGroupLabels);
114                 if (additionalGrantedPermissionCount > 0) {
115                     // N additional permissions.
116                     list.add(res.getQuantityString(
117                             R.plurals.runtime_permissions_additional_count,
118                             additionalGrantedPermissionCount, additionalGrantedPermissionCount));
119                 }
120                 if (list.size() == 0) {
121                     mSummary = res.getString(
122                             R.string.runtime_permissions_summary_no_permissions_granted);
123                 } else {
124                     mSummary = ListFormatter.getInstance().format(list);
125                 }
126             }
127             if (mViewHolder != null) {
128                 bindViewHolder(mViewHolder);
129             }
130         }
131     };
132 }
133