PreferredServices.java revision 2f07e3e221cb157918c198e4cd58d8242e897b68
1/*
2 * Copyright (C) 2014 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.nfc.cardemulation;
17
18import java.io.FileDescriptor;
19import java.io.PrintWriter;
20import java.util.ArrayList;
21
22import com.android.nfc.ForegroundUtils;
23
24import android.app.ActivityManager;
25import android.content.ComponentName;
26import android.content.Context;
27import android.database.ContentObserver;
28import android.net.Uri;
29import android.nfc.cardemulation.ApduServiceInfo;
30import android.nfc.cardemulation.CardEmulation;
31import android.os.Handler;
32import android.os.Looper;
33import android.os.UserHandle;
34import android.provider.Settings;
35import android.provider.Settings.SettingNotFoundException;
36import android.util.Log;
37
38/**
39 * This class keeps track of what HCE/SE-based services are
40 * preferred by the user. It currently has 3 inputs:
41 * 1) The default set in tap&pay menu for payment category
42 * 2) An app in the foreground asking for a specific
43 *    service for a specific category
44 * 3) If we had to disambiguate a previous tap (because no
45 *    preferred service was there), we need to temporarily
46 *    store the user's choice for the next tap.
47 *
48 * This class keeps track of all 3 inputs, and computes a new
49 * preferred services as needed. It then passes this service
50 * (if it changed) through a callback, which allows other components
51 * to adapt as necessary (ie the AID cache can update its AID
52 * mappings and the routing table).
53 */
54public class PreferredServices implements com.android.nfc.ForegroundUtils.Callback {
55    static final String TAG = "PreferredCardEmulationServices";
56    static final Uri paymentDefaultUri = Settings.Secure.getUriFor(
57            Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
58    static final Uri paymentForegroundUri = Settings.Secure.getUriFor(
59            Settings.Secure.NFC_PAYMENT_FOREGROUND);
60
61    final SettingsObserver mSettingsObserver;
62    final Context mContext;
63    final RegisteredServicesCache mServiceCache;
64    final Callback mCallback;
65    final ForegroundUtils mForegroundUtils = ForegroundUtils.getInstance();
66    final Handler mHandler = new Handler(Looper.getMainLooper());
67
68    final class PaymentDefaults {
69        boolean preferForeground; // The current selection mode for this category
70        ComponentName settingsDefault; // The component preferred in settings (eg Tap&Pay)
71        ComponentName currentPreferred; // The computed preferred component
72    }
73
74    final Object mLock = new Object();
75    // Variables below synchronized on mLock
76    PaymentDefaults mPaymentDefaults = new PaymentDefaults();
77
78    ComponentName mForegroundRequested; // The component preferred by fg app
79    int mForegroundUid; // The UID of the fg app, or -1 if fg app didn't request
80
81    ComponentName mNextTapDefault; // The component preferred by active disambig dialog
82    boolean mClearNextTapDefault = false; // Set when the next tap default must be cleared
83
84    ComponentName mForegroundCurrent; // The currently computed foreground component
85
86    public interface Callback {
87        void onPreferredPaymentServiceChanged(ComponentName service);
88        void onPreferredForegroundServiceChanged(ComponentName service);
89    }
90
91    public PreferredServices(Context context, RegisteredServicesCache serviceCache,
92            Callback callback) {
93        mContext = context;
94        mServiceCache = serviceCache;
95        mCallback = callback;
96        mSettingsObserver = new SettingsObserver(mHandler);
97        mContext.getContentResolver().registerContentObserver(
98                paymentDefaultUri,
99                true, mSettingsObserver, UserHandle.USER_ALL);
100
101        mContext.getContentResolver().registerContentObserver(
102                paymentForegroundUri,
103                true, mSettingsObserver, UserHandle.USER_ALL);
104
105        // Load current settings defaults for payments
106        loadDefaultsFromSettings(ActivityManager.getCurrentUser());
107    }
108
109    private final class SettingsObserver extends ContentObserver {
110        public SettingsObserver(Handler handler) {
111            super(handler);
112        }
113
114        @Override
115        public void onChange(boolean selfChange, Uri uri) {
116            super.onChange(selfChange, uri);
117            // Do it just for the current user. If it was in fact
118            // a change made for another user, we'll sync it down
119            // on user switch.
120            int currentUser = ActivityManager.getCurrentUser();
121            loadDefaultsFromSettings(currentUser);
122        }
123    };
124
125    void loadDefaultsFromSettings(int userId) {
126        boolean paymentDefaultChanged = false;
127        boolean paymentPreferForegroundChanged = false;
128        // Load current payment default from settings
129        String name = Settings.Secure.getStringForUser(
130                mContext.getContentResolver(), Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
131                userId);
132        ComponentName newDefault = name != null ? ComponentName.unflattenFromString(name) : null;
133        boolean preferForeground = false;
134        try {
135            preferForeground = Settings.Secure.getInt(mContext.getContentResolver(),
136                    Settings.Secure.NFC_PAYMENT_FOREGROUND) != 0;
137        } catch (SettingNotFoundException e) {
138        }
139        synchronized (mLock) {
140            paymentPreferForegroundChanged = (preferForeground != mPaymentDefaults.preferForeground);
141            mPaymentDefaults.preferForeground = preferForeground;
142
143            mPaymentDefaults.settingsDefault = newDefault;
144            if (newDefault != null && !newDefault.equals(mPaymentDefaults.currentPreferred)) {
145                paymentDefaultChanged = true;
146                mPaymentDefaults.currentPreferred = newDefault;
147            } else if (newDefault == null && mPaymentDefaults.currentPreferred != null) {
148                paymentDefaultChanged = true;
149                mPaymentDefaults.currentPreferred = newDefault;
150            } else {
151                // Same default as before
152            }
153        }
154        // Notify if anything changed
155        if (paymentDefaultChanged) {
156            mCallback.onPreferredPaymentServiceChanged(newDefault);
157        }
158        if (paymentPreferForegroundChanged) {
159            computePreferredForegroundService();
160        }
161    }
162
163    void computePreferredForegroundService() {
164        ComponentName preferredService = null;
165        boolean changed = false;
166        synchronized (mLock) {
167            // Prio 1: next tap default
168            preferredService = mNextTapDefault;
169            if (preferredService == null) {
170                // Prio 2: foreground requested by app
171                preferredService = mForegroundRequested;
172            }
173            if (preferredService != null && !preferredService.equals(mForegroundCurrent)) {
174                mForegroundCurrent = preferredService;
175                changed = true;
176            } else if (preferredService == null && mForegroundCurrent != null){
177                mForegroundCurrent = preferredService;
178                changed = true;
179            }
180        }
181        // Notify if anything changed
182        if (changed) {
183            mCallback.onPreferredForegroundServiceChanged(preferredService);
184        }
185    }
186
187    public boolean setDefaultForNextTap(ComponentName service) {
188        // This is a trusted API, so update without checking
189        synchronized (mLock) {
190            mNextTapDefault = service;
191        }
192        computePreferredForegroundService();
193        return true;
194    }
195
196    public boolean registerPreferredForegroundService(ComponentName service, int callingUid) {
197        boolean success = false;
198        ApduServiceInfo serviceInfo = mServiceCache.getService(ActivityManager.getCurrentUser(),
199                service);
200        synchronized (mLock) {
201            // Do some sanity checking
202            if (!mPaymentDefaults.preferForeground) {
203                // Foreground apps are not allowed to override payment default
204                // Check if this app registers payment AIDs, in which case we'll fail anyway
205                if (serviceInfo.hasCategory(CardEmulation.CATEGORY_PAYMENT)) {
206                    Log.d(TAG, "User doesn't allow payment services to be overridden.");
207                    return false;
208                }
209                // If no payment AIDs, get AIDs of category other, and see if there's any
210                // conflict with payment AIDs of current default payment app. That means
211                // the current default payment app said this was a payment AID, and the
212                // foreground app says it was not. In this case we'll still prefer the payment
213                // app, since that is the one that the user has explicitly selected (and said
214                // it's not allowed to be overridden).
215                final ArrayList<String> otherAids = serviceInfo.getAids();
216                ApduServiceInfo paymentServiceInfo = mServiceCache.getService(
217                        ActivityManager.getCurrentUser(), mPaymentDefaults.currentPreferred);
218                if (paymentServiceInfo != null && otherAids != null && otherAids.size() > 0) {
219                    for (String aid : otherAids) {
220                        if (CardEmulation.CATEGORY_PAYMENT.equals(
221                                paymentServiceInfo.getCategoryForAid(aid))) {
222                            Log.e(TAG, "AID " + aid + " is registered by the default payment app, " +
223                                    "and the user has not allowed payments to be overridden.");
224                            return false;
225                        }
226                    }
227                } else {
228                    // Could not find payment service or fg app doesn't register other AIDs;
229                    // okay to proceed.
230                }
231            }
232            if (mForegroundUtils.registerUidToBackgroundCallback(this, callingUid)) {
233                mForegroundRequested = service;
234                mForegroundUid = callingUid;
235                success = true;
236            } else {
237                Log.e(TAG, "Calling UID is not in the foreground, ignorning!");
238                success = false;
239            }
240        }
241        if (success) {
242            computePreferredForegroundService();
243        }
244        return success;
245    }
246
247    boolean unregisterForegroundService(int uid) {
248        boolean success = false;
249        synchronized (mLock) {
250            if (mForegroundUid == uid) {
251                mForegroundRequested = null;
252                mForegroundUid = -1;
253                success = true;
254            } // else, other UID in foreground
255        }
256        if (success) {
257            computePreferredForegroundService();
258        }
259        return success;
260    }
261
262    public boolean unregisteredPreferredForegroundService(int callingUid) {
263        // Verify the calling UID is in the foreground
264        if (mForegroundUtils.isInForeground(callingUid)) {
265            return unregisterForegroundService(callingUid);
266        } else {
267            Log.e(TAG, "Calling UID is not in the foreground, ignorning!");
268            return false;
269        }
270    }
271
272    @Override
273    public void onUidToBackground(int uid) {
274        unregisterForegroundService(uid);
275    }
276
277    public void onHostEmulationActivated() {
278        synchronized (mLock) {
279            mClearNextTapDefault = (mNextTapDefault != null);
280        }
281    }
282
283    public void onHostEmulationDeactivated() {
284        // If we had any next tap defaults set, clear them out
285        boolean changed = false;
286        synchronized (mLock) {
287            if (mClearNextTapDefault) {
288                // The reason we need to check this boolean is because the next tap
289                // default may have been set while the user held the phone
290                // on the reader; when the user then removes his phone from
291                // the reader (causing the "onHostEmulationDeactivated" event),
292                // the next tap default would immediately be cleared
293                // again. Instead, clear out defaults only if a next tap default
294                // had already been set at time of activation, which is captured
295                // by mClearNextTapDefault.
296                if (mNextTapDefault != null) {
297                    mNextTapDefault = null;
298                    changed = true;
299                }
300                mClearNextTapDefault = false;
301            }
302        }
303        if (changed) {
304            computePreferredForegroundService();
305        }
306    }
307
308    public void onUserSwitched(int userId) {
309        loadDefaultsFromSettings(userId);
310    }
311
312    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
313        pw.println("Preferred services (in order of importance): ");
314        pw.println("    *** Current preferred foreground service: " + mForegroundCurrent);
315        pw.println("    *** Current preferred payment service: " + mPaymentDefaults.currentPreferred);
316        pw.println("        Next tap default: " + mNextTapDefault);
317        pw.println("        Default for foreground app (UID: " + mForegroundUid +
318                "): " + mForegroundRequested);
319        pw.println("        Default in payment settings: " + mPaymentDefaults.settingsDefault);
320        pw.println("        Payment settings allows override: " + mPaymentDefaults.preferForeground);
321        pw.println("");
322    }
323}
324