summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/FrontDoorPlugin.java
blob: 51e686c79f1ac8a24f5e4abf95a1e8b66eb76255 (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2008 Esmertec AG.
 * Copyright (C) 2008 The Android Open Source Project
 *
 * 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.im.app;

import com.android.im.plugin.ImPluginConstants;

import android.app.Service;
import android.content.Intent;
import android.content.ContentUris;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.im.IImPlugin;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.Bundle;
import android.provider.Im;
import android.util.Log;
import android.text.TextUtils;
import android.database.Cursor;
import android.net.Uri;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

import dalvik.system.PathClassLoader;


public class FrontDoorPlugin extends Service {
    private final static String TAG = ImApp.LOG_TAG;
    private final static boolean LOCAL_DEBUG = false;

    // database access constants for branding resource map cache table
    private final static String[] BRANDING_RESOURCE_MAP_CACHE_PROJECTION = {
        Im.BrandingResourceMapCache.PROVIDER_ID,
        Im.BrandingResourceMapCache.APP_RES_ID,
        Im.BrandingResourceMapCache.PLUGIN_RES_ID
    };
    private final static int BRANDING_RESOURCE_MAP_CACHE_PROVIDER_ID_COLUMN = 0;
    private final static int BRANDING_RESOURCE_MAP_CACHE_APP_RES_ID_COLUMN = 1;
    private final static int BRANDING_RESOURCE_MAP_CACHE_PLUGIN_RES_ID_COLUMN = 2;

    private ArrayList<String> mProviderNames;
    private HashMap<String, String> mPackageNames;
    private HashMap<String, String> mClassNames;
    private HashMap<String, String> mSrcPaths;
    private HashMap<String, Map<Integer, Integer>> mBrandingResources;

    @Override
    public IBinder onBind(Intent intent) {
        // temporary provider ID<->Name mappings
        HashMap<String, Long> providerNameToId = new HashMap<String, Long>();
        HashMap<Long, String> providerIdToName = new HashMap<Long, String>();

        loadThirdPartyPlugins(providerNameToId, providerIdToName);
        loadBrandingResources(providerNameToId, providerIdToName);

        return mBinder;
    }

    private void loadThirdPartyPlugins(HashMap<String, Long> providerNameToId,
            HashMap<Long, String> providerIdToName) {
        mProviderNames = new ArrayList<String>();
        mPackageNames = new HashMap<String, String>();
        mClassNames = new HashMap<String, String>();
        mSrcPaths = new HashMap<String, String>();

        PackageManager pm = getPackageManager();
        List<ResolveInfo> plugins = pm.queryIntentServices(
                new Intent(ImPluginConstants.PLUGIN_ACTION_NAME), PackageManager.GET_META_DATA);
        for (ResolveInfo info : plugins) {
            if (LOCAL_DEBUG) log("loadThirdPartyPlugins: found plugin " + info);

            ServiceInfo serviceInfo = info.serviceInfo;
            if (serviceInfo == null) {
                Log.e(TAG, "loadThirdPartyPlugins: ignore bad plugin: " + info);
                continue;
            }

            String providerName = null;
            String providerFullName = null;
            String signUpUrl = null;
            Bundle metaData = serviceInfo.metaData;
            if (metaData != null) {
                providerName = metaData.getString(ImPluginConstants.METADATA_PROVIDER_NAME);
                providerFullName = metaData.getString(ImPluginConstants.METADATA_PROVIDER_FULL_NAME);
                signUpUrl = metaData.getString(ImPluginConstants.METADATA_SIGN_UP_URL);
            }
            if (TextUtils.isEmpty(providerName) || TextUtils.isEmpty(providerFullName)) {
                Log.e(TAG, "Ignore bad IM plugin: " + info + ". Lack of required meta data");
                continue;
            }

            mProviderNames.add(providerName);
            mPackageNames.put(providerName, serviceInfo.packageName);
            mClassNames.put(providerName, serviceInfo.name);
            mSrcPaths.put(providerName, serviceInfo.applicationInfo.sourceDir);

            long providerId = updateProviderDb(providerName, providerFullName, signUpUrl);
            providerNameToId.put(providerName, providerId);
            providerIdToName.put(providerId, providerName);
        }
    }

    private long updateProviderDb(String providerName, String providerFullName, String signUpUrl) {
        long providerId;
        ContentResolver cr = getContentResolver();
        String where = Im.Provider.NAME + "=?";
        String[] selectionArgs = new String[]{providerName};
        Cursor c = cr.query(Im.Provider.CONTENT_URI, null, where, selectionArgs, null);

        try {
            if (c.moveToFirst()) {
                providerId = c.getLong(c.getColumnIndexOrThrow(Im.Provider._ID));
                String origFullName = c.getString(
                        c.getColumnIndexOrThrow(Im.Provider.FULLNAME));
                String origCategory = c.getString(
                        c.getColumnIndexOrThrow(Im.Provider.CATEGORY));
                String origSignupUrl = c.getString(
                        c.getColumnIndexOrThrow(Im.Provider.SIGNUP_URL));
                ContentValues values = new ContentValues();
                if (origFullName == null || !origFullName.equals(providerFullName)) {
                    values.put(Im.Provider.FULLNAME, providerFullName);
                }
                if (origCategory == null) {
                    values.put(Im.Provider.CATEGORY, ImApp.IMPS_CATEGORY);
                }
                if (origSignupUrl == null || !origSignupUrl.equals(signUpUrl)) {
                    values.put(Im.Provider.SIGNUP_URL, signUpUrl);
                }
                if (values.size() > 0) {
                    Uri uri = ContentUris.withAppendedId(Im.Provider.CONTENT_URI, providerId);
                    cr.update(uri, values, null, null);
                }
            } else {
                ContentValues values = new ContentValues(3);
                values.put(Im.Provider.NAME, providerName);
                values.put(Im.Provider.FULLNAME, providerFullName);
                values.put(Im.Provider.CATEGORY, ImApp.IMPS_CATEGORY);
                values.put(Im.Provider.SIGNUP_URL, signUpUrl);

                Uri result = cr.insert(Im.Provider.CONTENT_URI, values);
                providerId = ContentUris.parseId(result);
            }
        } finally {
            c.close();
        }

        return providerId;
    }

    private void loadBrandingResources(HashMap<String, Long> providerNameToId,
            HashMap<Long, String> providerIdToName) {
        mBrandingResources = new HashMap<String, Map<Integer, Integer>>();

        if (loadBrandingResourcesFromCache(providerIdToName) <= 0) {
            Log.w(TAG, "Can't load from cache. Load from plugins...");
            loadBrandingResourcesFromPlugins();
            saveBrandingResourcesToCache(providerNameToId);
        }
    }

    /**
     * Try loading the branding resources from the database.
     * @param providerIdToName a map between provider ID and name.
     * @return 0 if the resources are not cached yet; otherwise the total count of res id
     * pairs.
     */
    private int loadBrandingResourcesFromCache(HashMap<Long, String> providerIdToName) {
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(
                Im.BrandingResourceMapCache.CONTENT_URI, /* URI */
                BRANDING_RESOURCE_MAP_CACHE_PROJECTION,  /* projection */
                null,                                    /* where */
                null,                                    /* where args */
                null                                     /* sort */);

        int count = 0;
        if (c != null) {
            try {
                while (c.moveToNext()) {
                    long providerId = c.getLong(BRANDING_RESOURCE_MAP_CACHE_PROVIDER_ID_COLUMN);
                    String provider = providerIdToName.get(providerId);
                    if (TextUtils.isEmpty(provider)) {
                        Log.e(TAG, "Empty provider name in branding resource map cache table.");
                        continue;
                    }
                    int appResId = c.getInt(BRANDING_RESOURCE_MAP_CACHE_APP_RES_ID_COLUMN);
                    int pluginResId = c.getInt(BRANDING_RESOURCE_MAP_CACHE_PLUGIN_RES_ID_COLUMN);

                    Map<Integer, Integer> resMap = mBrandingResources.get(provider);
                    if (resMap == null) {
                        resMap = new HashMap<Integer, Integer>();
                        mBrandingResources.put(provider, resMap);
                    }

                    resMap.put(appResId, pluginResId);

                    count++;
                }
            } finally {
                c.close();
            }
        } else {
            Log.e(TAG, "Query of branding resource map cache table returns empty cursor"); 
        }

        return count;
    }

    /**
     * Cache the loaded branding resources in IM database table, so that we can use it
     * directly and save loading time.
     * @param providerNameToId a map between provider name and ID.
     */
    private void saveBrandingResourcesToCache(HashMap<String, Long> providerNameToId) {
        ContentResolver cr = getContentResolver();

        ArrayList<ContentValues> valuesList = new ArrayList<ContentValues>();
        for (String provider : mBrandingResources.keySet()) {
            long providerId = providerNameToId.get(provider);

            Map<Integer, Integer> resMap = mBrandingResources.get(provider);
            for (int appResId : resMap.keySet()) {
                int pluginResId = resMap.get(appResId);

                ContentValues values = new ContentValues();
                values.put(Im.BrandingResourceMapCache.PROVIDER_ID, providerId);
                values.put(Im.BrandingResourceMapCache.APP_RES_ID, appResId);
                values.put(Im.BrandingResourceMapCache.PLUGIN_RES_ID, pluginResId);

                valuesList.add(values);
            }
        }

        int size = valuesList.size();
        if (size > 0) {
            cr.bulkInsert(
                    Im.BrandingResourceMapCache.CONTENT_URI,
                    valuesList.toArray(new ContentValues[size]));
        }
    }

    /**
     * Load the branding resources from all plugin packages.
     */
    private void loadBrandingResourcesFromPlugins() {
        for (String provider : mProviderNames) {
            if (!mBrandingResources.containsKey(provider)) {
                if (LOCAL_DEBUG) log("loadBrandingResources: load resource map for " + provider);
                Map<Integer, Integer> map = loadBrandingResource(mClassNames.get(provider),
                        mSrcPaths.get(provider));
                if (map != null) {
                    mBrandingResources.put(provider, map);
                }
            }
        }
    }

    /**
     * Load branding resources from one plugin package.
     */
    private Map<Integer, Integer> loadBrandingResource(String className, String srcPath) {
        Map retVal = null;

        if (LOCAL_DEBUG) log("loadBrandingResource: className=" + className +
                ", srcPath=" + srcPath);

        PathClassLoader classLoader = new PathClassLoader(srcPath,
                getCustomClassLoader());

        try {
            Class cls = classLoader.loadClass(className);
            Method m = cls.getMethod("getResourceMap");

            // TODO: this would still cause a VM verifier exception to be thrown if.
            // the landing page Android.mk and AndroidManifest.xml don't include use-library for
            // "com.android.im.plugin". This is even with getCustomClassLoader() as the parent
            // class loader.
            retVal = (Map)m.invoke(cls.newInstance(), new Object[]{});

        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (InstantiationException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (SecurityException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        } catch (InvocationTargetException e) {
            Log.e(TAG, "Failed load the plugin resource map", e);
        }

        return retVal;
    }

    private ClassLoader getCustomClassLoader() {
        /*
        // TODO: should not hard code the path!
        ClassLoader retVal = new PathClassLoader("/System/framework/com.android.im.plugin.jar",
                getClassLoader());
        if (LOCAL_DEBUG) log("getCustomClassLoader: " + retVal);
        return retVal;
        */
        return getClassLoader();
    }

    private void log(String msg) {
        Log.d(TAG, "[ImFrontDoor] " + msg);
    }


    /**
     * The implementation of IImFrontDoorPlugin defined through AIDL.
     */
    private final IImPlugin.Stub mBinder = new IImPlugin.Stub() {

        /**
         * Notify the plugin the front door activity is created. This gives the plugin a chance to
         * start its own servics, etc.
         */
        public void onStart() {
        }

        /**
         * Notify the plugin the front door activity is stopping.
         */
        public void onStop() {
        }

        /**
         * Sign in to the service for the account passed in.
         */
        public void signIn(long account) {
            if (LOCAL_DEBUG) log("signIn for account " + account);

            Intent intent = new Intent();
            intent.setData(ContentUris.withAppendedId(Im.Account.CONTENT_URI, account));
            intent.setClassName("com.android.im", "com.android.im.app.SigningInActivity");

            startActivity(intent);
        }

        /**
         * Sign out of the service for the account passed in.
         */
        public void signOut(long account) {
            if (LOCAL_DEBUG) log("signOut for account " + account);
            Intent intent = new Intent();
            intent.setData(ContentUris.withAppendedId(Im.Account.CONTENT_URI, account));
            intent.setClassName("com.android.im", "com.android.im.app.SignoutActivity");

            startActivity(intent);
        }

        public String getResourcePackageNameForProvider(String providerName) {
            return mPackageNames.get(providerName);
        }

        public Map getResourceMapForProvider(String providerName) throws RemoteException {
            return mBrandingResources.get(providerName);
        }

        public List getSupportedProviders() {
            return mProviderNames;
        }
    };

}