1 /*
2  * Copyright 2018 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.remotecallback;
17 
18 import static androidx.remotecallback.BroadcastReceiverWithCallbacks.ACTION_BROADCAST_CALLBACK;
19 import static androidx.remotecallback.RemoteCallback.EXTRA_METHOD;
20 import static androidx.remotecallback.RemoteCallback.TYPE_RECEIVER;
21 
22 import android.appwidget.AppWidgetProvider;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 
28 import androidx.annotation.RestrictTo;
29 
30 import org.jspecify.annotations.NonNull;
31 import org.jspecify.annotations.Nullable;
32 
33 /**
34  * Version of {@link AppWidgetProvider} that implements a {@link CallbackReceiver}.
35  *
36  * @param <T> Should be specified as the root class (e.g. class X extends
37  *           AppWidgetProviderWithCallbacks\<X>)
38  *
39  * @deprecated Slice framework has been deprecated, it will not receive any updates moving
40  * forward. If you are looking for a framework that handles communication across apps,
41  * consider using {@link android.app.appsearch.AppSearchManager}.
42  */
43 @Deprecated
44 @SuppressWarnings("HiddenSuperclass")
45 public class AppWidgetProviderWithCallbacks<T extends CallbackReceiver> extends
46         AppWidgetProvider implements CallbackReceiver<T>, CallbackBase<T> {
47 
48     @Override
onReceive(Context context, Intent intent)49     public void onReceive(Context context, Intent intent) {
50         if (ACTION_BROADCAST_CALLBACK.equals(intent.getAction())) {
51             CallbackHandlerRegistry.sInstance.invokeCallback(context, this, intent);
52         } else {
53             super.onReceive(context, intent);
54         }
55     }
56 
57     @Override
createRemoteCallback(@onNull Context context)58     public @NonNull T createRemoteCallback(@NonNull Context context) {
59         return CallbackHandlerRegistry.sInstance.getAndResetStub(getClass(), context, null);
60     }
61 
62     /**
63      */
64     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
65     @Override
toRemoteCallback(@onNull Class<T> cls, @NonNull Context context, @Nullable String authority, @NonNull Bundle args, @NonNull String method)66     public @NonNull RemoteCallback toRemoteCallback(@NonNull Class<T> cls, @NonNull Context context,
67             @Nullable String authority,
68             @NonNull Bundle args, @NonNull String method) {
69         Intent intent = new Intent(ACTION_BROADCAST_CALLBACK);
70         intent.setComponent(new ComponentName(context.getPackageName(), cls.getName()));
71         args.putString(EXTRA_METHOD, method);
72         intent.putExtras(args);
73         return new RemoteCallback(context, TYPE_RECEIVER, intent, cls.getName(), args);
74     }
75 }
76