summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/map/BluetoothMapAppObserver.java
blob: a1b78ddf69f99a1a5c91333043d51a492b1410a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright (C) 2014 Samsung System LSI
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.bluetooth.map;

import com.android.bluetooth.R;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import com.android.bluetooth.mapapi.BluetoothMapContract;
import android.util.Log;

/**
 * Class to construct content observers for for email applications on the system.
 *
 *
 */

public class BluetoothMapAppObserver{

    private static final String TAG = "BluetoothMapAppObserver";

    private static final boolean D = BluetoothMapService.DEBUG;
    private static final boolean V = Log.isLoggable(BluetoothMapService.LOG_TAG, Log.VERBOSE);
    /*  */
    private LinkedHashMap<BluetoothMapAccountItem, ArrayList<BluetoothMapAccountItem>> mFullList;
    private LinkedHashMap<String,ContentObserver> mObserverMap =
            new LinkedHashMap<String,ContentObserver>();
    private ContentResolver mResolver;
    private Context mContext;
    private BroadcastReceiver mReceiver;
    private PackageManager mPackageManager = null;
    BluetoothMapAccountLoader mLoader;
    BluetoothMapService mMapService = null;
    private boolean mRegisteredReceiver = false;

    public BluetoothMapAppObserver(final Context context, BluetoothMapService mapService) {
        mContext    = context;
        mMapService = mapService;
        mResolver   = context.getContentResolver();
        boolean isDisabledNonAosp = context.getResources().getBoolean
                (R.bool.disable_non_aosp_bt_features);
        if (D) Log.d(TAG, "isDisabledNonAosp :" + isDisabledNonAosp);
        if (isDisabledNonAosp) {
            mLoader = new BluetoothMapAccountLoader(mContext);
        } else {
            mLoader = new BluetoothMapAccountEmailLoader(mContext);
        }
        mFullList   = mLoader.parsePackages(false); /* Get the current list of apps */
        createReceiver();
        initObservers();
    }


    private BluetoothMapAccountItem getApp(String authoritiesName) {
        if(V) Log.d(TAG, "getApp(): Looking for " + authoritiesName);
        for(BluetoothMapAccountItem app:mFullList.keySet()){
            if(V) Log.d(TAG, "  Comparing: " + app.getProviderAuthority());
            if(app.getProviderAuthority().equals(authoritiesName)) {
                if(V) Log.d(TAG, "  found " + app.mBase_uri_no_account);
                return app;
            }
        }
        if(V) Log.d(TAG, "  NOT FOUND!");
        return null;
    }

    private void handleAccountChanges(String packageNameWithProvider) {

        if(D)Log.d(TAG,"handleAccountChanges (packageNameWithProvider: "
                        +packageNameWithProvider+"\n");
        //String packageName = packageNameWithProvider.replaceFirst("\\.[^\\.]+$", "");
        BluetoothMapAccountItem app = getApp(packageNameWithProvider);
        if(app != null) {
            ArrayList<BluetoothMapAccountItem> newAccountList = mLoader.parseAccounts(app);
            ArrayList<BluetoothMapAccountItem> oldAccountList = mFullList.get(app);
            ArrayList<BluetoothMapAccountItem> addedAccountList =
                    (ArrayList<BluetoothMapAccountItem>)newAccountList.clone();
            // Same as oldAccountList.clone
            ArrayList<BluetoothMapAccountItem> removedAccountList = mFullList.get(app);
            if (oldAccountList == null)
                oldAccountList = new ArrayList <BluetoothMapAccountItem>();
            if (removedAccountList == null)
                removedAccountList = new ArrayList <BluetoothMapAccountItem>();

            mFullList.put(app, newAccountList);
            for(BluetoothMapAccountItem newAcc: newAccountList){
                for(BluetoothMapAccountItem oldAcc: oldAccountList){
                    if(newAcc.getId() == oldAcc.getId()){
                        // For each match remove from both removed and added lists
                        removedAccountList.remove(oldAcc);
                        addedAccountList.remove(newAcc);
                        if(!newAcc.getName().equals(oldAcc.getName()) && newAcc.mIsChecked){
                            // Name Changed and the acc is visible - Change Name in SDP record
                            mMapService.updateMasInstances(
                                    BluetoothMapService.UPDATE_MAS_INSTANCES_ACCOUNT_RENAMED);
                            if(V)Log.d(TAG, "    UPDATE_MAS_INSTANCES_ACCOUNT_RENAMED");
                        }
                        if(newAcc.mIsChecked != oldAcc.mIsChecked) {
                            // Visibility changed
                            if(newAcc.mIsChecked){
                                // account added - create SDP record
                                mMapService.updateMasInstances(
                                        BluetoothMapService.UPDATE_MAS_INSTANCES_ACCOUNT_ADDED);
                                if(V)Log.d(TAG, "UPDATE_MAS_INSTANCES_ACCOUNT_ADDED " +
                                        "isChecked changed");
                            } else {
                                // account removed - remove SDP record
                                mMapService.updateMasInstances(
                                        BluetoothMapService.UPDATE_MAS_INSTANCES_ACCOUNT_REMOVED);
                                if(V)Log.d(TAG, "    UPDATE_MAS_INSTANCES_ACCOUNT_REMOVED " +
                                        "isChecked changed");
                            }
                        }
                        break;
                    }
                }
            }
            // Notify on any removed accounts
            for(BluetoothMapAccountItem removedAcc: removedAccountList){
                mMapService.updateMasInstances(
                        BluetoothMapService.UPDATE_MAS_INSTANCES_ACCOUNT_REMOVED);
                if(V)Log.d(TAG, "    UPDATE_MAS_INSTANCES_ACCOUNT_REMOVED " + removedAcc);
            }
            // Notify on any new accounts
            for(BluetoothMapAccountItem addedAcc: addedAccountList){
                mMapService.updateMasInstances(
                        BluetoothMapService.UPDATE_MAS_INSTANCES_ACCOUNT_ADDED);
                if(V)Log.d(TAG, "    UPDATE_MAS_INSTANCES_ACCOUNT_ADDED " + addedAcc);
            }

        } else {
            Log.e(TAG, "Received change notification on package not registered for notifications!");

        }
    }

    /**
     * Adds a new content observer to the list of content observers.
     * The key for the observer is the uri as string
     * @param uri uri for the package that supports MAP email
     */

    public void registerObserver(BluetoothMapAccountItem app) {
        Uri uri = BluetoothMapContract.buildAccountUri(app.getProviderAuthority());
        if (V) Log.d(TAG, "registerObserver for URI "+uri.toString()+"\n");
        ContentObserver observer = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                onChange(selfChange, null);
            }

            @Override
            public void onChange(boolean selfChange, Uri uri) {
                if (V) Log.d(TAG, "onChange on thread: " + Thread.currentThread().getId()
                        + " Uri: " + uri + " selfchange: " + selfChange);
                if(uri != null) {
                    handleAccountChanges(uri.getHost());
                } else {
                    Log.e(TAG, "Unable to handle change as the URI is NULL!");
                }

            }
        };
        mObserverMap.put(uri.toString(), observer);
        //False "notifyForDescendents" : Get notified whenever a change occurs to the exact URI.
        mResolver.registerContentObserver(uri, false, observer);
    }

    public void unregisterObserver(BluetoothMapAccountItem app) {
        Uri uri = BluetoothMapContract.buildAccountUri(app.getProviderAuthority());
        if (V) Log.d(TAG, "unregisterObserver("+uri.toString()+")\n");
        mResolver.unregisterContentObserver(mObserverMap.get(uri.toString()));
        mObserverMap.remove(uri.toString());
    }

    private void initObservers(){
        if(D)Log.d(TAG,"initObservers()");
        for(BluetoothMapAccountItem app: mFullList.keySet()){
            registerObserver(app);
        }
    }

    private void deinitObservers(){
        if(D)Log.d(TAG,"deinitObservers()");
        for(BluetoothMapAccountItem app: mFullList.keySet()){
            unregisterObserver(app);
        }
    }

    private void createReceiver(){
        if(D)Log.d(TAG,"createReceiver()\n");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(D)Log.d(TAG,"onReceive\n");
                String action = intent.getAction();

                if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
                    Uri data = intent.getData();
                    String packageName = data.getEncodedSchemeSpecificPart();
                    if(D)Log.d(TAG,"The installed package is: "+ packageName);

                    BluetoothMapUtils.TYPE msgType = BluetoothMapUtils.TYPE.NONE;
                    ResolveInfo resolveInfo = null;
                    Intent[] searchIntents = new Intent[2];
                    //Array <Intent> searchIntents = new Array <Intent>();
                    searchIntents[0] = new Intent(BluetoothMapContract.PROVIDER_INTERFACE_EMAIL);
                    searchIntents[1] = new Intent(BluetoothMapContract.PROVIDER_INTERFACE_IM);
                    // Find all installed packages and filter out those that support Bluetooth Map.

                    mPackageManager = mContext.getPackageManager();

                    for (Intent searchIntent : searchIntents) {
                        List<ResolveInfo> resInfos =
                                mPackageManager.queryIntentContentProviders(searchIntent, 0);
                        if (resInfos != null ) {
                            if(D) Log.d(TAG,"Found " + resInfos.size()
                                    + " application(s) with intent "
                                    + searchIntent.getAction().toString());
                            for (ResolveInfo rInfo : resInfos) {
                                if(rInfo != null) {
                                    // Find out if package contain Bluetooth MAP support
                                    if (packageName.equals(rInfo.providerInfo.packageName)) {
                                        resolveInfo = rInfo;
                                        if(searchIntent.getAction() ==
                                                BluetoothMapContract.PROVIDER_INTERFACE_EMAIL){
                                            msgType = BluetoothMapUtils.TYPE.EMAIL;
                                        } else if (searchIntent.getAction() ==
                                                BluetoothMapContract.PROVIDER_INTERFACE_IM){
                                            msgType = BluetoothMapUtils.TYPE.IM;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    // if application found with Bluetooth MAP support add to list
                    if(resolveInfo != null) {
                        if(D) Log.d(TAG,"Found " + resolveInfo.providerInfo.packageName
                                + " application of type " + msgType);
                        BluetoothMapAccountItem app = mLoader.createAppItem(resolveInfo,
                                false, msgType);
                        if(app != null) {
                            registerObserver(app);
                            // Add all accounts to mFullList
                            ArrayList<BluetoothMapAccountItem> newAccountList =
                                    mLoader.parseAccounts(app);
                            mFullList.put(app, newAccountList);
                        }
                    }

                }
                else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
                    Uri data = intent.getData();
                    String packageName = data.getEncodedSchemeSpecificPart();
                    if(D)Log.d(TAG,"The removed package is: "+ packageName);
                    BluetoothMapAccountItem app = getApp(packageName);
                    /* Find the object and remove from fullList */
                    if(app != null) {
                        unregisterObserver(app);
                        mFullList.remove(app);
                    }
                }
            }
        };
        if (!mRegisteredReceiver) {
            try {
                mContext.registerReceiver(mReceiver,intentFilter);
                mRegisteredReceiver = true;
            } catch (Exception e) {
                Log.e(TAG,"Unable to register MapAppObserver receiver", e);
            }
        }
    }

    private void removeReceiver(){
        if(D)Log.d(TAG,"removeReceiver()\n");
        if (mRegisteredReceiver) {
            try {
                mRegisteredReceiver = false;
                mContext.unregisterReceiver(mReceiver);
            } catch (Exception e) {
                Log.e(TAG,"Unable to unregister mapAppObserver receiver", e);
            }
        }
    }

    /**
     * Method to get a list of the accounts (across all apps) that are set to be shared
     * through MAP.
     * @return Arraylist<BluetoothMapAccountItem> containing all enabled accounts
     */
    public ArrayList<BluetoothMapAccountItem> getEnabledAccountItems(){
        if(D)Log.d(TAG,"getEnabledAccountItems()\n");
        ArrayList<BluetoothMapAccountItem> list = new ArrayList<BluetoothMapAccountItem>();
        for (BluetoothMapAccountItem app:mFullList.keySet()){
            if (app != null) {
                ArrayList<BluetoothMapAccountItem> accountList = mFullList.get(app);
                if (accountList != null) {
                    for (BluetoothMapAccountItem acc: accountList) {
                        if (acc.mIsChecked) {
                            list.add(acc);
                        }
                    }
                } else {
                    Log.w(TAG,"getEnabledAccountItems() - No AccountList enabled\n");
                }
            } else {
                Log.w(TAG,"getEnabledAccountItems() - No Account in App enabled\n");
            }
        }
        return list;
    }

    /**
     * Method to get a list of the accounts (across all apps).
     * @return Arraylist<BluetoothMapAccountItem> containing all accounts
     */
    public ArrayList<BluetoothMapAccountItem> getAllAccountItems(){
        if(D)Log.d(TAG,"getAllAccountItems()\n");
        ArrayList<BluetoothMapAccountItem> list = new ArrayList<BluetoothMapAccountItem>();
        for(BluetoothMapAccountItem app:mFullList.keySet()){
            ArrayList<BluetoothMapAccountItem> accountList = mFullList.get(app);
            list.addAll(accountList);
        }
        return list;
    }


    /**
     * Cleanup all resources - must be called to avoid leaks.
     */
    public void shutdown() {
        deinitObservers();
        removeReceiver();
    }
}