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 android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ActivityInfo;
22 import android.content.pm.ComponentInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Parcel;
26 import android.util.Log;
27 
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * Description of a single dashboard tile which is generated from an activity.
33  */
34 public class ActivityTile extends Tile {
35     private static final String TAG = "ActivityTile";
36 
ActivityTile(ActivityInfo info, String category)37     public ActivityTile(ActivityInfo info, String category) {
38         super(info, category, info.metaData);
39     }
40 
ActivityTile(Parcel in)41     ActivityTile(Parcel in) {
42         super(in);
43     }
44 
45     @Override
getId()46     public int getId() {
47         return Objects.hash(getPackageName(), getComponentName());
48     }
49 
50     @Override
getDescription()51     public String getDescription() {
52         return getPackageName() + "/" + getComponentName();
53     }
54 
55     @Override
getComponentInfo(Context context)56     protected ComponentInfo getComponentInfo(Context context) {
57         if (mComponentInfo == null) {
58             final PackageManager pm = context.getApplicationContext().getPackageManager();
59             final Intent intent = getIntent();
60             final List<ResolveInfo> infoList =
61                     pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
62             if (infoList != null && !infoList.isEmpty()) {
63                 mComponentInfo = infoList.get(0).activityInfo;
64                 setMetaData(mComponentInfo.metaData);
65             } else {
66                 Log.e(TAG, "Cannot find package info for "
67                         + intent.getComponent().flattenToString());
68             }
69         }
70         return mComponentInfo;
71     }
72 
73     @Override
getComponentLabel(Context context)74     protected CharSequence getComponentLabel(Context context) {
75         final PackageManager pm = context.getPackageManager();
76         final ComponentInfo info = getComponentInfo(context);
77         return info == null
78                 ? null
79                 : info.loadLabel(pm);
80     }
81 
82     @Override
getComponentIcon(ComponentInfo componentInfo)83     protected int getComponentIcon(ComponentInfo componentInfo) {
84         return componentInfo.icon;
85     }
86 }
87