1c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn/*
2c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * Copyright (C) 2013 The Android Open Source Project
3c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn *
4c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * you may not use this file except in compliance with the License.
6c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * You may obtain a copy of the License at
7c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn *
8c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn *
10c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * See the License for the specific language governing permissions and
14c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn * limitations under the License.
15c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn */
16c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
17c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornpackage com.google.android.test.shared_library;
18c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
19c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornimport android.app.Activity;
20ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackbornimport android.app.Fragment;
21ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackbornimport android.app.FragmentManager;
22c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornimport android.content.Context;
23c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornimport android.content.pm.PackageInfo;
24c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornimport android.content.pm.PackageManager;
25c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
26c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackbornpublic class SharedLibraryMain {
27ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn    static String LIBRARY_PACKAGE = "com.google.android.test.shared_library";
28c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
29c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    /**
30c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn     * Base version of the library.
31c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn     */
32c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    public static int VERSION_BASE = 1;
33c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
34c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    /**
35c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn     * The second version of the library.
36c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn     */
37c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    public static int VERSION_SECOND = 2;
38c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
39ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn    /**
40ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     * Return the version number of the currently installed library.
41ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     */
42c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    public static int getVersion(Context context) {
43c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        PackageInfo pi = null;
44c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        try {
45c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn            pi = context.getPackageManager().getPackageInfo(LIBRARY_PACKAGE, 0);
46c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn            return pi.versionCode;
47c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        } catch (PackageManager.NameNotFoundException e) {
48c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn            throw new IllegalStateException("Can't find my package!", e);
49c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        }
50c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    }
51c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
52ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn    /**
53ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     * Check that the library's version is at least the given minimum version,
54ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     * displaying a dialog to have the user install an update if that is not true.
55ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     * The dialog is displayed as a DialogFragment in your activity if a newer
56ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     * version is needed.  If a newer version is needed, false is returned.
57ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn     */
58ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn    public static boolean ensureVersion(final Activity activity, int minVersion) {
59ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        final FragmentManager fm = activity.getFragmentManager();
60ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        final String dialogTag = LIBRARY_PACKAGE + ":version";
61ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        Fragment curDialog = fm.findFragmentByTag(dialogTag);
62ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn
63c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        if (getVersion(activity) >= minVersion) {
64ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            // Library version is sufficient.  Make sure any version dialog
65ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            // we had shown is removed before returning.
66ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            if (curDialog != null) {
67ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn                fm.beginTransaction().remove(curDialog).commitAllowingStateLoss();
68ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            }
69ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            return true;
70c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        }
71c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
72ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        // The current version of the library does not meet the required version.
73ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        // If we don't already have a version dialog displayed, display it now.
74ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        if (curDialog == null) {
75ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            curDialog = new VersionDialog();
76ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn            fm.beginTransaction().add(curDialog, dialogTag).commitAllowingStateLoss();
77c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn        }
78c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn
79ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        // Tell the caller that the current version is not sufficient.
80ce5abb0a5542a0dae00a2af3b174d83cdd85a21fDianne Hackborn        return false;
81c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn    }
82c895be7bc68b6f5b37fbb9881f464dd5ea0eb017Dianne Hackborn}
83