SharedLibraryMain.java revision c895be7bc68b6f5b37fbb9881f464dd5ea0eb017
1/*
2 * Copyright (C) 2013 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
17package com.google.android.test.shared_library;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager;
26import android.content.res.Resources;
27
28public class SharedLibraryMain {
29    private static String LIBRARY_PACKAGE = "com.google.android.test.shared_library";
30
31    /**
32     * Base version of the library.
33     */
34    public static int VERSION_BASE = 1;
35
36    /**
37     * The second version of the library.
38     */
39    public static int VERSION_SECOND = 2;
40
41    public static int getVersion(Context context) {
42        PackageInfo pi = null;
43        try {
44            pi = context.getPackageManager().getPackageInfo(LIBRARY_PACKAGE, 0);
45            return pi.versionCode;
46        } catch (PackageManager.NameNotFoundException e) {
47            throw new IllegalStateException("Can't find my package!", e);
48        }
49    }
50
51    public static void ensureVersion(Activity activity, int minVersion) {
52        if (getVersion(activity) >= minVersion) {
53            return;
54        }
55
56        // The current version of the library does not meet the required version.  Show
57        // a dialog to inform the user and have them update to the current version.
58        // Note that updating the library will be necessity mean killing the current
59        // application (so it can be re-started with the new version, so there is no
60        // reason to return a result here.
61        final Context context;
62        try {
63            context = activity.createPackageContext(LIBRARY_PACKAGE, 0);
64        } catch (PackageManager.NameNotFoundException e) {
65            throw new IllegalStateException("Can't find my package!", e);
66        }
67
68        // Display the dialog.  Note that we don't need to deal with activity lifecycle
69        // stuff because if the activity gets recreated, it will first call through to
70        // ensureVersion(), causing us to either re-display the dialog if needed or let
71        // it now proceed.
72        final Resources res = context.getResources();
73        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
74        builder.setTitle(res.getText(R.string.upgrade_title));
75        builder.setMessage(res.getString(R.string.upgrade_body,
76                activity.getApplicationInfo().loadLabel(activity.getPackageManager()),
77                context.getApplicationInfo().loadLabel(context.getPackageManager())));
78        builder.setPositiveButton(res.getText(R.string.upgrade_button),
79                new Dialog.OnClickListener() {
80                    @Override
81                    public void onClick(DialogInterface dialog, int which) {
82                        // Launch play store.
83                    }
84                });
85        builder.show();
86    }
87}
88