Home | History | Annotate | Download | only in shared_library
      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 
     17 package com.google.android.test.shared_library;
     18 
     19 import android.app.Activity;
     20 import android.app.Fragment;
     21 import android.app.FragmentManager;
     22 import android.content.Context;
     23 import android.content.pm.PackageInfo;
     24 import android.content.pm.PackageManager;
     25 
     26 public class SharedLibraryMain {
     27     static String LIBRARY_PACKAGE = "com.google.android.test.shared_library";
     28 
     29     /**
     30      * Base version of the library.
     31      */
     32     public static int VERSION_BASE = 1;
     33 
     34     /**
     35      * The second version of the library.
     36      */
     37     public static int VERSION_SECOND = 2;
     38 
     39     /**
     40      * Return the version number of the currently installed library.
     41      */
     42     public static int getVersion(Context context) {
     43         PackageInfo pi = null;
     44         try {
     45             pi = context.getPackageManager().getPackageInfo(LIBRARY_PACKAGE, 0);
     46             return pi.versionCode;
     47         } catch (PackageManager.NameNotFoundException e) {
     48             throw new IllegalStateException("Can't find my package!", e);
     49         }
     50     }
     51 
     52     /**
     53      * Check that the library's version is at least the given minimum version,
     54      * displaying a dialog to have the user install an update if that is not true.
     55      * The dialog is displayed as a DialogFragment in your activity if a newer
     56      * version is needed.  If a newer version is needed, false is returned.
     57      */
     58     public static boolean ensureVersion(final Activity activity, int minVersion) {
     59         final FragmentManager fm = activity.getFragmentManager();
     60         final String dialogTag = LIBRARY_PACKAGE + ":version";
     61         Fragment curDialog = fm.findFragmentByTag(dialogTag);
     62 
     63         if (getVersion(activity) >= minVersion) {
     64             // Library version is sufficient.  Make sure any version dialog
     65             // we had shown is removed before returning.
     66             if (curDialog != null) {
     67                 fm.beginTransaction().remove(curDialog).commitAllowingStateLoss();
     68             }
     69             return true;
     70         }
     71 
     72         // The current version of the library does not meet the required version.
     73         // If we don't already have a version dialog displayed, display it now.
     74         if (curDialog == null) {
     75             curDialog = new VersionDialog();
     76             fm.beginTransaction().add(curDialog, dialogTag).commitAllowingStateLoss();
     77         }
     78 
     79         // Tell the caller that the current version is not sufficient.
     80         return false;
     81     }
     82 }
     83