1 /*
2  * Copyright (C) 2019 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.settingslib.drawer;
18 
19 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ComponentInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ProviderInfo;
26 import android.content.pm.ResolveInfo;
27 import android.os.Bundle;
28 import android.os.Parcel;
29 import android.util.Log;
30 
31 import java.util.List;
32 import java.util.Objects;
33 
34 /**
35  * Description of a single dashboard tile which is generated from a content provider.
36  */
37 public class ProviderTile extends Tile {
38     private static final String TAG = "ProviderTile";
39 
40     private static final boolean DEBUG_TIMING = false;
41 
42     private String mAuthority;
43     private String mKey;
44 
ProviderTile(ProviderInfo info, String category, Bundle metaData)45     public ProviderTile(ProviderInfo info, String category, Bundle metaData) {
46         super(info, category, metaData);
47         mAuthority = info.authority;
48         mKey = metaData.getString(META_DATA_PREFERENCE_KEYHINT);
49     }
50 
ProviderTile(Parcel in)51     ProviderTile(Parcel in) {
52         super(in);
53         mAuthority = ((ProviderInfo) mComponentInfo).authority;
54         mKey = getMetaData().getString(META_DATA_PREFERENCE_KEYHINT);
55     }
56 
57     @Override
getId()58     public int getId() {
59         return Objects.hash(mAuthority, mKey);
60     }
61 
62     @Override
getDescription()63     public String getDescription() {
64         return mAuthority + "/" + mKey;
65     }
66 
67     @Override
getComponentInfo(Context context)68     protected ComponentInfo getComponentInfo(Context context) {
69         if (mComponentInfo == null) {
70             final long startTime = System.currentTimeMillis();
71             final PackageManager pm = context.getApplicationContext().getPackageManager();
72             final Intent intent = getIntent();
73             final List<ResolveInfo> infoList =
74                     pm.queryIntentContentProviders(intent, 0 /* flags */);
75             if (infoList != null && !infoList.isEmpty()) {
76                 final ProviderInfo providerInfo = infoList.get(0).providerInfo;
77                 mComponentInfo = providerInfo;
78                 setMetaData(TileUtils.getEntryDataFromProvider(context, providerInfo.authority,
79                         mKey));
80             } else {
81                 Log.e(TAG, "Cannot find package info for "
82                         + intent.getComponent().flattenToString());
83             }
84 
85             if (DEBUG_TIMING) {
86                 Log.d(TAG, "getComponentInfo took "
87                         + (System.currentTimeMillis() - startTime) + " ms");
88             }
89         }
90         return mComponentInfo;
91     }
92 
93     @Override
getComponentLabel(Context context)94     protected CharSequence getComponentLabel(Context context) {
95         // Getting provider label for a tile title isn't supported.
96         return null;
97     }
98 
99     @Override
getComponentIcon(ComponentInfo info)100     protected int getComponentIcon(ComponentInfo info) {
101         // Getting provider icon for a tile title isn't supported.
102         return 0;
103     }
104 }
105