Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package android.app.slice; |
| 18 | |
| 19 | import android.annotation.SystemService; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 20 | import android.app.slice.ISliceListener.Stub; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 22 | import android.net.Uri; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 23 | import android.os.Handler; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 24 | import android.os.RemoteException; |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 25 | import android.os.ServiceManager; |
| 26 | import android.os.ServiceManager.ServiceNotFoundException; |
| 27 | |
| 28 | /** |
| 29 | * @hide |
| 30 | */ |
| 31 | @SystemService(Context.SLICE_SERVICE) |
| 32 | public class SliceManager { |
| 33 | |
| 34 | private final ISliceManager mService; |
| 35 | private final Context mContext; |
| 36 | |
| 37 | public SliceManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 38 | mContext = context; |
| 39 | mService = ISliceManager.Stub.asInterface( |
| 40 | ServiceManager.getServiceOrThrow(Context.SLICE_SERVICE)); |
| 41 | } |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | */ |
| 45 | public void addSliceListener(Uri uri, SliceListener listener, SliceSpec[] specs) { |
| 46 | try { |
| 47 | mService.addSliceListener(uri, mContext.getPackageName(), listener.mStub, specs); |
| 48 | } catch (RemoteException e) { |
| 49 | throw e.rethrowFromSystemServer(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | */ |
| 55 | public void removeSliceListener(Uri uri, SliceListener listener) { |
| 56 | try { |
| 57 | mService.removeSliceListener(uri, mContext.getPackageName(), listener.mStub); |
| 58 | } catch (RemoteException e) { |
| 59 | throw e.rethrowFromSystemServer(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | */ |
| 65 | public void pinSlice(Uri uri, SliceSpec[] specs) { |
| 66 | try { |
| 67 | mService.pinSlice(mContext.getPackageName(), uri, specs); |
| 68 | } catch (RemoteException e) { |
| 69 | throw e.rethrowFromSystemServer(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | */ |
| 75 | public void unpinSlice(Uri uri) { |
| 76 | try { |
| 77 | mService.unpinSlice(mContext.getPackageName(), uri); |
| 78 | } catch (RemoteException e) { |
| 79 | throw e.rethrowFromSystemServer(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | */ |
| 85 | public boolean hasSliceAccess() { |
| 86 | try { |
| 87 | return mService.hasSliceAccess(mContext.getPackageName()); |
| 88 | } catch (RemoteException e) { |
| 89 | throw e.rethrowFromSystemServer(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | */ |
| 95 | public SliceSpec[] getPinnedSpecs(Uri uri) { |
| 96 | try { |
| 97 | return mService.getPinnedSpecs(uri, mContext.getPackageName()); |
| 98 | } catch (RemoteException e) { |
| 99 | throw e.rethrowFromSystemServer(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | */ |
| 105 | public abstract static class SliceListener { |
| 106 | private final Handler mHandler; |
| 107 | |
| 108 | /** |
| 109 | */ |
| 110 | public SliceListener() { |
| 111 | this(Handler.getMain()); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | */ |
| 116 | public SliceListener(Handler h) { |
| 117 | mHandler = h; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | */ |
| 122 | public abstract void onSliceUpdated(Slice s); |
| 123 | |
| 124 | private final ISliceListener.Stub mStub = new Stub() { |
| 125 | @Override |
| 126 | public void onSliceUpdated(Slice s) throws RemoteException { |
| 127 | mHandler.post(() -> SliceListener.this.onSliceUpdated(s)); |
| 128 | } |
| 129 | }; |
| 130 | } |
Jason Monk | 8f5f7ff | 2017-10-17 14:12:42 -0400 | [diff] [blame] | 131 | } |