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 
17 package com.android.systemui.plugin.globalactions.wallet;
18 
19 import static android.provider.Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
20 import static android.provider.Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
21 
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.provider.Settings;
25 import android.service.quickaccesswallet.QuickAccessWalletClient;
26 
27 import com.android.internal.annotations.VisibleForTesting;
28 import com.android.systemui.plugins.GlobalActionsPanelPlugin;
29 import com.android.systemui.plugins.annotations.Requirements;
30 import com.android.systemui.plugins.annotations.Requires;
31 
32 @Requirements({
33         @Requires(
34                 target = GlobalActionsPanelPlugin.class,
35                 version = GlobalActionsPanelPlugin.VERSION),
36         @Requires(
37                 target = GlobalActionsPanelPlugin.Callbacks.class,
38                 version = GlobalActionsPanelPlugin.Callbacks.VERSION),
39         @Requires(
40                 target = GlobalActionsPanelPlugin.PanelViewController.class,
41                 version = GlobalActionsPanelPlugin.PanelViewController.VERSION),
42 })
43 public class WalletPluginService implements GlobalActionsPanelPlugin {
44 
45     private Context mSysuiContext;
46     private Context mPluginContext;
47 
48     @Override
onCreate(Context sysuiContext, Context pluginContext)49     public void onCreate(Context sysuiContext, Context pluginContext) {
50         onCreate(sysuiContext, pluginContext, QuickAccessWalletClient.create(sysuiContext));
51     }
52 
53     @VisibleForTesting
onCreate(Context sysuiContext, Context pluginContext, QuickAccessWalletClient client)54     void onCreate(Context sysuiContext, Context pluginContext, QuickAccessWalletClient client) {
55         mSysuiContext = sysuiContext;
56         mPluginContext = pluginContext;
57         updateSettingsFeatureAvailability(mSysuiContext, client.isWalletServiceAvailable());
58     }
59 
60 
61     /**
62      * Invoked when the GlobalActions menu is shown.
63      *
64      * @param callbacks    {@link Callbacks} instance that can be used by the Panel to interact with
65      *                     the Global Actions menu.
66      * @param deviceLocked Indicates whether or not the device is currently locked.
67      * @return A {@link PanelViewController} instance used to receive Global Actions events.
68      */
69     @Override
onPanelShown( GlobalActionsPanelPlugin.Callbacks callbacks, boolean deviceLocked)70     public PanelViewController onPanelShown(
71             GlobalActionsPanelPlugin.Callbacks callbacks, boolean deviceLocked) {
72         return onPanelShown(callbacks, deviceLocked, QuickAccessWalletClient.create(mSysuiContext));
73     }
74 
75     @VisibleForTesting
onPanelShown( GlobalActionsPanelPlugin.Callbacks callbacks, boolean isDeviceLocked, QuickAccessWalletClient client)76     GlobalActionsPanelPlugin.PanelViewController onPanelShown(
77             GlobalActionsPanelPlugin.Callbacks callbacks,
78             boolean isDeviceLocked,
79             QuickAccessWalletClient client) {
80         boolean serviceAvailable = client.isWalletServiceAvailable();
81         updateSettingsFeatureAvailability(mSysuiContext, serviceAvailable);
82         if (!serviceAvailable || !client.isWalletFeatureAvailable()) {
83             return null;
84         }
85         WalletPanelViewController panelViewController = new WalletPanelViewController(
86                 mSysuiContext, mPluginContext, client, callbacks, isDeviceLocked);
87         panelViewController.queryWalletCards();
88         return panelViewController;
89     }
90 
91 
92     @Override
onDestroy()93     public void onDestroy() {
94         mSysuiContext = null;
95         mPluginContext = null;
96     }
97 
98     /**
99      * Enabling GLOBAL_ACTIONS_PANEL_AVAILABLE makes the settings screen visible. Enabling
100      * GLOBAL_ACTIONS_PANEL_ENABLED when the settings is not set effectively turns the feature on by
101      * default.
102      */
103     @VisibleForTesting
updateSettingsFeatureAvailability(Context context, boolean available)104     static void updateSettingsFeatureAvailability(Context context, boolean available) {
105         ContentResolver cr = context.getContentResolver();
106         // Turning on the availability toggle lets users turn the feature on and off in Settings
107         Settings.Secure.putInt(cr, GLOBAL_ACTIONS_PANEL_AVAILABLE, available ? 1 : 0);
108         // Enable the panel by default, but do not re-enable if the user has disabled it.
109         if (Settings.Secure.getInt(cr, GLOBAL_ACTIONS_PANEL_ENABLED, -1) == -1) {
110             Settings.Secure.putInt(cr, GLOBAL_ACTIONS_PANEL_ENABLED, 1);
111         }
112     }
113 }
114