• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.wallpaper.util;
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.os.Bundle;
26 import android.os.Handler;
27 import android.os.Looper;
28 import android.text.TextUtils;
29 
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 
33 /** Util class for wallpaper preview. */
34 public class PreviewUtils {
35 
36     private static final String PREVIEW = "preview";
37     private static final String METHOD_GET_PREVIEW = "get_preview";
38     private static final ExecutorService sExecutorService = Executors.newSingleThreadExecutor();
39 
40     private final Context mContext;
41     private final String mProviderAuthority;
42     private ProviderInfo mProviderInfo;
43 
PreviewUtils(Context context, String authorityMetadataKey)44     public PreviewUtils(Context context, String authorityMetadataKey) {
45         mContext = context;
46         Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
47 
48         ResolveInfo info = context.getPackageManager().resolveActivity(homeIntent,
49                 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
50         if (info != null && info.activityInfo != null && info.activityInfo.metaData != null) {
51             mProviderAuthority = info.activityInfo.metaData.getString(authorityMetadataKey);
52         } else {
53             mProviderAuthority = null;
54         }
55 
56         mProviderInfo = TextUtils.isEmpty(mProviderAuthority) ? null
57                 : mContext.getPackageManager().resolveContentProvider(mProviderAuthority, 0);
58         if (mProviderInfo != null && !TextUtils.isEmpty(mProviderInfo.readPermission)) {
59             if (context.checkSelfPermission(mProviderInfo.readPermission)
60                     != PackageManager.PERMISSION_GRANTED) {
61                 mProviderInfo = null;
62             }
63         }
64     }
65 
66     /**
67      * Render preview under the current grid option.
68      * @param bundle request options to pass on the call
69      * @param callback to receive the results, it will be called on the main thread.
70      */
renderPreview(Bundle bundle, WorkspacePreviewCallback callback)71     public void renderPreview(Bundle bundle, WorkspacePreviewCallback callback) {
72         sExecutorService.submit(() -> {
73             Bundle result = mContext.getContentResolver().call(getUri(PREVIEW),
74                     METHOD_GET_PREVIEW, null, bundle);
75             new Handler(Looper.getMainLooper()).post(() -> callback.onPreviewRendered(result));
76         });
77     }
78 
79     /** Easy way to generate a Uri with the provider info from this class. */
getUri(String path)80     public Uri getUri(String path) {
81         return new Uri.Builder()
82                 .scheme(ContentResolver.SCHEME_CONTENT)
83                 .authority(mProviderInfo.authority)
84                 .appendPath(path)
85                 .build();
86     }
87 
88     /** Return whether preview is supported. */
supportsPreview()89     public boolean supportsPreview() {
90         return mProviderInfo != null;
91     }
92 
93     /**
94      * Callback for a call to the provider to render preview
95      */
96     public interface WorkspacePreviewCallback {
97         /**
98          * Called with the result from the provider.
99          */
onPreviewRendered(Bundle resultBundle)100         void onPreviewRendered(Bundle resultBundle);
101     }
102 }
103