blob: e3ced7193f6935e2edc7e832ea9b2e3ef4dd0de6 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.wallpaper.module;
17
18import android.content.Context;
19import android.content.Intent;
20import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
22import android.net.Uri;
23import android.os.AsyncTask;
Jon Miranda16ea1b12017-12-12 14:52:48 -080024
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28
Sunny Goyal8600a3f2018-08-15 12:48:01 -070029import androidx.annotation.Nullable;
30
Jon Miranda16ea1b12017-12-12 14:52:48 -080031/**
32 * Checks whether an explore action can be taken for the given uri, i.e. whether any activity on
33 * the device is capable of handling it.
34 */
35public class DefaultExploreIntentChecker implements ExploreIntentChecker {
36 private Context mAppContext;
37
38 private Map<Uri, Intent> mUriToActionViewIntentMap;
39
40 public DefaultExploreIntentChecker(Context appContext) {
41 mAppContext = appContext;
42 mUriToActionViewIntentMap = new HashMap<>();
43 }
44
45 @Override
46 public void fetchValidActionViewIntent(Uri uri, IntentReceiver receiver) {
47 if (mUriToActionViewIntentMap.containsKey(uri)) {
48 receiver.onIntentReceived(mUriToActionViewIntentMap.get(uri));
49 return;
50 }
51
52 new FetchActionViewIntentTask(mAppContext, uri, receiver).execute();
53 }
54
55 private class FetchActionViewIntentTask extends AsyncTask<Void, Void, Intent> {
56 private Context mAppContext;
57 private Uri mUri;
58 private IntentReceiver mReceiver;
59
60 public FetchActionViewIntentTask(Context appContext, Uri uri, IntentReceiver receiver) {
61 mAppContext = appContext;
62 mUri = uri;
63 mReceiver = receiver;
64 }
65
66 @Nullable
67 @Override
68 protected Intent doInBackground(Void... params) {
69 Intent actionViewIntent = new Intent(Intent.ACTION_VIEW, mUri);
70
71 PackageManager pm = mAppContext.getPackageManager();
72 List<ResolveInfo> activities = pm.queryIntentActivities(actionViewIntent, /* flags */ 0);
73
74 Intent result = activities.isEmpty() ? null : actionViewIntent;
75 mUriToActionViewIntentMap.put(mUri, result);
76
77 return result;
78 }
79
80 @Override
81 protected void onPostExecute(Intent intent) {
82 mReceiver.onIntentReceived(intent);
83 }
84 }
85}
86