1 /*
2  * Copyright (C) 2016 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.documentsui.sidebar;
18 
19 import android.content.Intent;
20 import android.content.pm.ActivityInfo;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.os.UserManager;
24 import android.text.TextUtils;
25 import android.view.View;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import com.android.documentsui.ActionHandler;
30 import com.android.documentsui.IconUtils;
31 import com.android.documentsui.R;
32 import com.android.documentsui.base.UserId;
33 import com.android.documentsui.dirlist.AppsRowItemData;
34 
35 /**
36  * An {@link Item} for apps that supports some picking actions like
37  * {@link Intent#ACTION_GET_CONTENT} such as Photos. This is only used in pickers.
38  */
39 public class AppItem extends Item {
40     private static final String STRING_ID_FORMAT = "AppItem{%s/%s}";
41 
42     public final ResolveInfo info;
43 
44     private final ActionHandler mActionHandler;
45 
AppItem(ResolveInfo info, String title, UserId userId, ActionHandler actionHandler)46     public AppItem(ResolveInfo info, String title, UserId userId, ActionHandler actionHandler) {
47         super(R.layout.item_root, title, getStringId(info), userId);
48         this.info = info;
49         mActionHandler = actionHandler;
50     }
51 
getStringId(ResolveInfo info)52     private static String getStringId(ResolveInfo info) {
53         ActivityInfo activityInfo = info.activityInfo;
54 
55         String component = String.format(
56                 STRING_ID_FORMAT, activityInfo.applicationInfo.packageName, activityInfo.name);
57         return component;
58     }
59 
bindIcon(ImageView icon)60     protected void bindIcon(ImageView icon) {
61         final PackageManager pm = userId.getPackageManager(icon.getContext());
62         // This always gives badged icon if package manager is from a managed profile.
63         icon.setImageDrawable(info.loadIcon(pm));
64     }
65 
66 
bindActionIcon(View actionIconArea, ImageView actionIcon)67     protected void bindActionIcon(View actionIconArea, ImageView actionIcon) {
68         actionIconArea.setVisibility(View.VISIBLE);
69         actionIconArea.setFocusable(false);
70         actionIcon.setImageDrawable(
71                 IconUtils.applyTintColor(actionIcon.getContext(), R.drawable.ic_exit_to_app,
72                         R.color.item_action_icon));
73 
74     }
75 
76     @Override
showAppDetails()77     boolean showAppDetails() {
78         mActionHandler.showAppDetails(info, userId);
79         return true;
80     }
81 
82     @Override
bindView(View convertView)83     void bindView(View convertView) {
84         final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
85         final TextView titleView = (TextView) convertView.findViewById(android.R.id.title);
86         final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
87         final View actionIconArea = convertView.findViewById(R.id.action_icon_area);
88         final ImageView actionIcon = (ImageView) convertView.findViewById(R.id.action_icon);
89 
90         titleView.setText(title);
91         titleView.setContentDescription(userId.getUserBadgedLabel(convertView.getContext(), title));
92 
93         bindIcon(icon);
94         bindActionIcon(actionIconArea, actionIcon);
95 
96         // TODO: match existing summary behavior from disambig dialog
97         summary.setVisibility(View.GONE);
98     }
99 
100     @Override
isRoot()101     boolean isRoot() {
102         // We won't support drag n' drop in pickers, and apps only show up there.
103         return false;
104     }
105 
106     @Override
open()107     void open() {
108         mActionHandler.openRoot(info, userId);
109     }
110 
111     @Override
getPackageName()112     public String getPackageName() {
113         return info.activityInfo.packageName;
114     }
115 
116     @Override
toString()117     public String toString() {
118         return "AppItem{"
119                 + "id=" + stringId
120                 + ", userId=" + userId
121                 + ", resolveInfo=" + info
122                 + "}";
123     }
124 }
125