1 /*
2  * Copyright (C) 2018 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.dirlist;
18 
19 import android.content.Context;
20 import android.content.pm.ResolveInfo;
21 import android.graphics.drawable.Drawable;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.documentsui.ActionHandler;
26 import com.android.documentsui.base.RootInfo;
27 import com.android.documentsui.base.UserId;
28 import com.android.documentsui.sidebar.AppItem;
29 import com.android.documentsui.sidebar.Item;
30 import com.android.documentsui.sidebar.RootItem;
31 
32 /**
33  * A bacis data class stored data which apps row chip required.
34  * This is abstract class and it will be implemented by {@link AppData} and {@link RootData},
35  * both classes are different by the item is {@link AppItem} or {@link RootItem}.
36  */
37 public abstract class AppsRowItemData {
38 
39     protected final UserId mUserId;
40     private final String mTitle;
41     private final @Nullable String mSummary;
42     protected final ActionHandler mActionHandler;
43     protected final boolean mMaybeShowBadge;
44 
AppsRowItemData(Item item, ActionHandler actionHandler, boolean shouldShowSummary, boolean maybeShowBadge)45     public AppsRowItemData(Item item, ActionHandler actionHandler, boolean shouldShowSummary,
46             boolean maybeShowBadge) {
47         mUserId = item.userId;
48         mTitle = item.title;
49         mSummary = shouldShowSummary ? item.getSummary() : null;
50         mActionHandler = actionHandler;
51         mMaybeShowBadge = maybeShowBadge;
52     }
53 
getTitle()54     public final String getTitle() {
55         return mTitle;
56     }
57 
getUserId()58     public final UserId getUserId() {
59         return mUserId;
60     }
61 
62     /**
63      * Get the summary from {@link Item}.
64      */
getSummary()65     public final @Nullable String getSummary() {
66         return mSummary;
67     }
68 
getIconDrawable(Context context)69     protected abstract Drawable getIconDrawable(Context context);
onClicked()70     protected abstract void onClicked();
71 
72     public static class AppData extends AppsRowItemData {
73 
74         private final ResolveInfo mResolveInfo;
75 
AppData(AppItem item, ActionHandler actionHandler, boolean shouldShowSummary, boolean maybeShowBadge)76         public AppData(AppItem item, ActionHandler actionHandler, boolean shouldShowSummary,
77                 boolean maybeShowBadge) {
78             super(item, actionHandler, shouldShowSummary, maybeShowBadge);
79             mResolveInfo = item.info;
80         }
81 
82         @Override
getIconDrawable(Context context)83         protected Drawable getIconDrawable(Context context) {
84             return mResolveInfo.loadIcon(mUserId.getPackageManager(context));
85         }
86 
87         @Override
onClicked()88         protected void onClicked() {
89             mActionHandler.openRoot(mResolveInfo, mUserId);
90         }
91     }
92 
93     public static class RootData extends AppsRowItemData {
94 
95         private final RootInfo mRootInfo;
96 
RootData(RootItem item, ActionHandler actionHandler, boolean shouldShowSummary, boolean maybeShowBadge)97         public RootData(RootItem item, ActionHandler actionHandler, boolean shouldShowSummary,
98                 boolean maybeShowBadge) {
99             super(item, actionHandler, shouldShowSummary, maybeShowBadge);
100             mRootInfo = item.root;
101         }
102 
103         @Override
getIconDrawable(Context context)104         protected Drawable getIconDrawable(Context context) {
105             return mRootInfo.loadIcon(context, mMaybeShowBadge);
106         }
107 
108         @Override
onClicked()109         protected void onClicked() {
110             mActionHandler.openRoot(mRootInfo);
111         }
112     }
113 }
114