Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | */ |
| 16 | package androidx.app.slice.widget; |
| 17 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 18 | import android.arch.lifecycle.LiveData; |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 19 | import android.content.ContentProvider; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 21 | import android.content.Intent; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 22 | import android.database.ContentObserver; |
| 23 | import android.net.Uri; |
| 24 | import android.os.AsyncTask; |
| 25 | import android.os.Handler; |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 26 | import android.support.annotation.NonNull; |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 27 | import android.support.annotation.Nullable; |
| 28 | import android.support.annotation.RestrictTo; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 29 | |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 30 | import java.util.Arrays; |
| 31 | import java.util.List; |
| 32 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 33 | import androidx.app.slice.Slice; |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 34 | import androidx.app.slice.SliceSpec; |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 35 | import androidx.app.slice.SliceSpecs; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 36 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 37 | /** |
| 38 | * Class with factory methods for creating LiveData that observes slices. |
| 39 | * |
| 40 | * @see #fromUri(Context, Uri) |
| 41 | * @see LiveData |
| 42 | */ |
| 43 | public final class SliceLiveData { |
| 44 | |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 45 | private static final List<SliceSpec> SUPPORTED_SPECS = Arrays.asList(SliceSpecs.BASIC, |
Mady Mellor | c779101 | 2018-01-16 17:25:01 -0800 | [diff] [blame] | 46 | SliceSpecs.LIST); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Turns a slice Uri into slice content. |
| 50 | * |
| 51 | * @param context Context to be used. |
| 52 | * @param uri The URI to a slice provider |
| 53 | * @return The Slice provided by the app or null if none is given. |
| 54 | * @see Slice |
| 55 | * @hide |
| 56 | */ |
| 57 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 58 | public static @Nullable Slice bindSlice(Context context, @NonNull Uri uri) { |
| 59 | return Slice.bindSlice(context, uri, SUPPORTED_SPECS); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Turns a slice intent into slice content. Expects an explicit intent. If there is no |
| 65 | * {@link ContentProvider} associated with the given intent this will throw |
| 66 | * {@link IllegalArgumentException}. |
| 67 | * |
| 68 | * @param context The context to use. |
| 69 | * @param intent The intent associated with a slice. |
| 70 | * @return The Slice provided by the app or null if none is given. |
| 71 | * @see Slice |
| 72 | * @see androidx.app.slice.SliceProvider#onMapIntentToUri(Intent) |
| 73 | * @see Intent |
| 74 | * @hide |
| 75 | */ |
| 76 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 77 | public static @Nullable Slice bindSlice(Context context, @NonNull Intent intent) { |
| 78 | return Slice.bindSlice(context, intent, SUPPORTED_SPECS); |
| 79 | } |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 80 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 81 | /** |
| 82 | * Produces an {@link LiveData} that tracks a Slice for a given Uri. To use |
| 83 | * this method your app must have the permission to the slice Uri or hold |
| 84 | * {@link android.Manifest.permission#BIND_SLICE}). |
| 85 | */ |
| 86 | public static LiveData<Slice> fromUri(Context context, Uri uri) { |
| 87 | return new SliceLiveDataImpl(context.getApplicationContext(), uri); |
| 88 | } |
| 89 | |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 90 | /** |
| 91 | * Produces an {@link LiveData} that tracks a Slice for a given Intent. To use |
| 92 | * this method your app must have the permission to the slice Uri or hold |
| 93 | * {@link android.Manifest.permission#BIND_SLICE}). |
| 94 | */ |
| 95 | public static LiveData<Slice> fromIntent(@NonNull Context context, @NonNull Intent intent) { |
| 96 | return new SliceLiveDataImpl(context.getApplicationContext(), intent); |
| 97 | } |
| 98 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 99 | private static class SliceLiveDataImpl extends LiveData<Slice> { |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 100 | private final Context mContext; |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 101 | private final Intent mIntent; |
| 102 | private Uri mUri; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 103 | |
| 104 | private SliceLiveDataImpl(Context context, Uri uri) { |
| 105 | super(); |
| 106 | mContext = context; |
| 107 | mUri = uri; |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 108 | mIntent = null; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 109 | // TODO: Check if uri points at a Slice? |
| 110 | } |
| 111 | |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 112 | private SliceLiveDataImpl(Context context, Intent intent) { |
| 113 | super(); |
| 114 | mContext = context; |
| 115 | mUri = null; |
| 116 | mIntent = intent; |
| 117 | } |
| 118 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 119 | @Override |
| 120 | protected void onActive() { |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 121 | AsyncTask.execute(mUpdateSlice); |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 122 | if (mUri != null) { |
| 123 | mContext.getContentResolver().registerContentObserver(mUri, false, mObserver); |
| 124 | } |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void onInactive() { |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 129 | if (mUri != null) { |
| 130 | mContext.getContentResolver().unregisterContentObserver(mObserver); |
| 131 | } |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 134 | private final Runnable mUpdateSlice = new Runnable() { |
| 135 | @Override |
| 136 | public void run() { |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 137 | Slice s = mUri != null ? Slice.bindSlice(mContext, mUri, SUPPORTED_SPECS) |
| 138 | : Slice.bindSlice(mContext, mIntent, SUPPORTED_SPECS); |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 139 | if (mUri == null && s != null) { |
| 140 | mContext.getContentResolver().registerContentObserver(s.getUri(), |
| 141 | false, mObserver); |
| 142 | mUri = s.getUri(); |
| 143 | } |
| 144 | postValue(s); |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 145 | } |
| 146 | }; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 147 | |
| 148 | private final ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 149 | @Override |
| 150 | public void onChange(boolean selfChange) { |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 151 | AsyncTask.execute(mUpdateSlice); |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 152 | } |
| 153 | }; |
| 154 | } |
| 155 | } |