summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDianne Hackborn <hackbod@google.com>2013-03-15 11:37:01 -0700
committerDianne Hackborn <hackbod@google.com>2013-03-15 11:37:01 -0700
commitce5abb0a5542a0dae00a2af3b174d83cdd85a21f (patch)
tree9665772dc632b7680ff83d1e5707e3973b551be6
parent5d122d96a5bf3bbaccaca2765b45716efe7ee2ef (diff)
downloadframeworks_base-ce5abb0a5542a0dae00a2af3b174d83cdd85a21f.zip
frameworks_base-ce5abb0a5542a0dae00a2af3b174d83cdd85a21f.tar.gz
frameworks_base-ce5abb0a5542a0dae00a2af3b174d83cdd85a21f.tar.bz2
Improve/flesh-out shared library version check.
Change-Id: I3d6eb19c8aeaae07a6d17de81ac707c7741608fb
-rw-r--r--tests/SharedLibrary/lib/src/com/google/android/test/shared_library/SharedLibraryMain.java65
-rw-r--r--tests/SharedLibrary/lib/src/com/google/android/test/shared_library/VersionDialog.java72
2 files changed, 102 insertions, 35 deletions
diff --git a/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/SharedLibraryMain.java b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/SharedLibraryMain.java
index c1cd925..2c61b77 100644
--- a/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/SharedLibraryMain.java
+++ b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/SharedLibraryMain.java
@@ -17,16 +17,14 @@
package com.google.android.test.shared_library;
import android.app.Activity;
-import android.app.AlertDialog;
-import android.app.Dialog;
+import android.app.Fragment;
+import android.app.FragmentManager;
import android.content.Context;
-import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
-import android.content.res.Resources;
public class SharedLibraryMain {
- private static String LIBRARY_PACKAGE = "com.google.android.test.shared_library";
+ static String LIBRARY_PACKAGE = "com.google.android.test.shared_library";
/**
* Base version of the library.
@@ -38,6 +36,9 @@ public class SharedLibraryMain {
*/
public static int VERSION_SECOND = 2;
+ /**
+ * Return the version number of the currently installed library.
+ */
public static int getVersion(Context context) {
PackageInfo pi = null;
try {
@@ -48,40 +49,34 @@ public class SharedLibraryMain {
}
}
- public static void ensureVersion(Activity activity, int minVersion) {
+ /**
+ * Check that the library's version is at least the given minimum version,
+ * displaying a dialog to have the user install an update if that is not true.
+ * The dialog is displayed as a DialogFragment in your activity if a newer
+ * version is needed. If a newer version is needed, false is returned.
+ */
+ public static boolean ensureVersion(final Activity activity, int minVersion) {
+ final FragmentManager fm = activity.getFragmentManager();
+ final String dialogTag = LIBRARY_PACKAGE + ":version";
+ Fragment curDialog = fm.findFragmentByTag(dialogTag);
+
if (getVersion(activity) >= minVersion) {
- return;
+ // Library version is sufficient. Make sure any version dialog
+ // we had shown is removed before returning.
+ if (curDialog != null) {
+ fm.beginTransaction().remove(curDialog).commitAllowingStateLoss();
+ }
+ return true;
}
- // The current version of the library does not meet the required version. Show
- // a dialog to inform the user and have them update to the current version.
- // Note that updating the library will be necessity mean killing the current
- // application (so it can be re-started with the new version, so there is no
- // reason to return a result here.
- final Context context;
- try {
- context = activity.createPackageContext(LIBRARY_PACKAGE, 0);
- } catch (PackageManager.NameNotFoundException e) {
- throw new IllegalStateException("Can't find my package!", e);
+ // The current version of the library does not meet the required version.
+ // If we don't already have a version dialog displayed, display it now.
+ if (curDialog == null) {
+ curDialog = new VersionDialog();
+ fm.beginTransaction().add(curDialog, dialogTag).commitAllowingStateLoss();
}
- // Display the dialog. Note that we don't need to deal with activity lifecycle
- // stuff because if the activity gets recreated, it will first call through to
- // ensureVersion(), causing us to either re-display the dialog if needed or let
- // it now proceed.
- final Resources res = context.getResources();
- AlertDialog.Builder builder = new AlertDialog.Builder(activity);
- builder.setTitle(res.getText(R.string.upgrade_title));
- builder.setMessage(res.getString(R.string.upgrade_body,
- activity.getApplicationInfo().loadLabel(activity.getPackageManager()),
- context.getApplicationInfo().loadLabel(context.getPackageManager())));
- builder.setPositiveButton(res.getText(R.string.upgrade_button),
- new Dialog.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Launch play store.
- }
- });
- builder.show();
+ // Tell the caller that the current version is not sufficient.
+ return false;
}
}
diff --git a/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/VersionDialog.java b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/VersionDialog.java
new file mode 100644
index 0000000..f457532
--- /dev/null
+++ b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/VersionDialog.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2013 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.google.android.test.shared_library;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.net.Uri;
+import android.os.Bundle;
+
+/**
+ * This is the dialog we show when the library's version is older than
+ * the version the app needs.
+ */
+public class VersionDialog extends DialogFragment {
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ final Activity activity = getActivity();
+
+ // Need to use our library's resources for showing the dialog.
+ final Context context;
+ try {
+ context = activity.createPackageContext(SharedLibraryMain.LIBRARY_PACKAGE, 0);
+ } catch (PackageManager.NameNotFoundException e) {
+ throw new IllegalStateException("Can't find my package!", e);
+ }
+
+ final Resources res = context.getResources();
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ builder.setTitle(res.getText(R.string.upgrade_title));
+ builder.setMessage(res.getString(R.string.upgrade_body,
+ activity.getApplicationInfo().loadLabel(activity.getPackageManager()),
+ context.getApplicationInfo().loadLabel(context.getPackageManager())));
+ builder.setPositiveButton(res.getText(R.string.upgrade_button),
+ new Dialog.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ // Launch play store into the details of our app.
+ try {
+ activity.startActivity(new Intent(Intent.ACTION_VIEW,
+ Uri.parse("market://details?id="
+ + SharedLibraryMain.LIBRARY_PACKAGE)));
+ } catch (android.content.ActivityNotFoundException anfe) {
+ activity.startActivity(new Intent(Intent.ACTION_VIEW,
+ Uri.parse("http://play.google.com/store/apps/details?id="
+ + SharedLibraryMain.LIBRARY_PACKAGE)));
+ }
+ }
+ });
+ return builder.create();
+ }
+}