1 /*
2  * Copyright (C) 2021 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 package com.android.customization.model.themedicon;
17 
18 import android.content.ContentResolver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ProviderInfo;
23 import android.content.pm.ResolveInfo;
24 import android.net.Uri;
25 import android.text.TextUtils;
26 
27 /**
28  * Utility class for themed icon.
29  */
30 public class ThemedIconUtils {
31 
32     private final Context mContext;
33     private final String mProviderAuthority;
34 
35     private ProviderInfo mProviderInfo;
36 
ThemedIconUtils(Context context, String authorityMetaKey)37     public ThemedIconUtils(Context context, String authorityMetaKey) {
38         mContext = context;
39         Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
40         ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
41                 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
42 
43         if (resolveInfo != null && resolveInfo.activityInfo.metaData != null) {
44             mProviderAuthority = resolveInfo.activityInfo.metaData.getString(authorityMetaKey);
45         } else {
46             mProviderAuthority = null;
47         }
48         mProviderInfo = TextUtils.isEmpty(mProviderAuthority) ? null :
49                 mContext.getPackageManager().resolveContentProvider(mProviderAuthority, 0);
50         if (mProviderInfo != null && !TextUtils.isEmpty(mProviderInfo.readPermission)) {
51             if (mContext.checkSelfPermission(mProviderInfo.readPermission)
52                     != PackageManager.PERMISSION_GRANTED) {
53                 mProviderInfo = null;
54             }
55         }
56     }
57 
58     /**
59      * Gets the Uri for this provider's authority with path information.
60      *
61      * @param path the path segment of {@link Uri}
62      * @return {@link Uri} with path information
63      */
getUriForPath(String path)64     Uri getUriForPath(String path) {
65         return new Uri.Builder()
66                 .scheme(ContentResolver.SCHEME_CONTENT)
67                 .authority(mProviderInfo.authority)
68                 .appendPath(path)
69                 .build();
70     }
71 
72     /**
73      * Returns if themed icon is available.
74      *
75      * @return true if themed icon feature is available, false otherwise.
76      */
isThemedIconAvailable()77     boolean isThemedIconAvailable() {
78         return mProviderInfo != null;
79     }
80 }
81