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.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.content.Intent;
     26 import android.content.pm.PackageManager;
     27 import android.content.res.Resources;
     28 import android.net.Uri;
     29 import android.os.Bundle;
     30 
     31 /**
     32  * This is the dialog we show when the library's version is older than
     33  * the version the app needs.
     34  */
     35 public class VersionDialog extends DialogFragment {
     36     @Override
     37     public Dialog onCreateDialog(Bundle savedInstanceState) {
     38         final Activity activity = getActivity();
     39 
     40         // Need to use our library's resources for showing the dialog.
     41         final Context context;
     42         try {
     43             context = activity.createPackageContext(SharedLibraryMain.LIBRARY_PACKAGE, 0);
     44         } catch (PackageManager.NameNotFoundException e) {
     45             throw new IllegalStateException("Can't find my package!", e);
     46         }
     47 
     48         final Resources res = context.getResources();
     49         AlertDialog.Builder builder = new AlertDialog.Builder(activity);
     50         builder.setTitle(res.getText(R.string.upgrade_title));
     51         builder.setMessage(res.getString(R.string.upgrade_body,
     52                 activity.getApplicationInfo().loadLabel(activity.getPackageManager()),
     53                 context.getApplicationInfo().loadLabel(context.getPackageManager())));
     54         builder.setPositiveButton(res.getText(R.string.upgrade_button),
     55                 new Dialog.OnClickListener() {
     56                     @Override
     57                     public void onClick(DialogInterface dialog, int which) {
     58                         // Launch play store into the details of our app.
     59                         try {
     60                             activity.startActivity(new Intent(Intent.ACTION_VIEW,
     61                                     Uri.parse("market://details?id="
     62                                             + SharedLibraryMain.LIBRARY_PACKAGE)));
     63                         } catch (android.content.ActivityNotFoundException anfe) {
     64                             activity.startActivity(new Intent(Intent.ACTION_VIEW,
     65                                     Uri.parse("http://play.google.com/store/apps/details?id="
     66                                             + SharedLibraryMain.LIBRARY_PACKAGE)));
     67                         }
     68                     }
     69                 });
     70         return builder.create();
     71     }
     72 }
     73