Home | History | Annotate | Download | only in gms
      1 package org.robolectric.shadows.gms;
      2 
      3 import android.app.Activity;
      4 import android.app.Dialog;
      5 import android.app.PendingIntent;
      6 import android.content.Context;
      7 import android.content.DialogInterface.OnCancelListener;
      8 import android.content.Intent;
      9 import android.content.res.Resources;
     10 import android.support.v4.app.Fragment;
     11 import com.google.android.gms.common.ConnectionResult;
     12 import com.google.android.gms.common.GooglePlayServicesUtil;
     13 import com.google.common.base.Preconditions;
     14 import org.robolectric.RuntimeEnvironment;
     15 import org.robolectric.annotation.Implementation;
     16 import org.robolectric.annotation.Implements;
     17 import org.robolectric.annotation.Resetter;
     18 
     19 /**
     20  * Calls to static method of {@link GooglePlayServicesUtil} will be redirected to the provided
     21  * {@link GooglePlayServicesUtilImpl} implementation. Use
     22  * {@link #provideImpl(GooglePlayServicesUtilImpl)} to
     23  * set the implementation instance. By default, a {@link GooglePlayServicesUtilImpl} is used in call
     24  * redirection. Use mocks or subclassing {@link GooglePlayServicesUtilImpl} to achieve desired
     25  * behaviors.
     26  */
     27 @Implements(GooglePlayServicesUtil.class)
     28 public class ShadowGooglePlayServicesUtil {
     29   private static GooglePlayServicesUtilImpl googlePlayServicesUtilImpl =
     30       new GooglePlayServicesUtilImpl();
     31 
     32   public static synchronized GooglePlayServicesUtilImpl getImpl() {
     33     return googlePlayServicesUtilImpl;
     34   }
     35 
     36   public static synchronized void provideImpl(GooglePlayServicesUtilImpl impl) {
     37     googlePlayServicesUtilImpl = Preconditions.checkNotNull(impl);
     38   }
     39 
     40   @Resetter
     41   public static synchronized void reset() {
     42     googlePlayServicesUtilImpl = new GooglePlayServicesUtilImpl();
     43   }
     44 
     45   @Implementation
     46   public static synchronized Context getRemoteContext(Context context) {
     47     return googlePlayServicesUtilImpl.getRemoteContext(context);
     48   }
     49 
     50   @Implementation
     51   public static synchronized Resources getRemoteResource(Context context) {
     52     return googlePlayServicesUtilImpl.getRemoteResource(context);
     53   }
     54 
     55   @Implementation
     56   public static synchronized boolean showErrorDialogFragment(int errorCode, Activity activity,
     57       Fragment fragment, int requestCode, OnCancelListener cancelListener) {
     58     return googlePlayServicesUtilImpl.showErrorDialogFragment(
     59         errorCode, activity, fragment, requestCode, cancelListener);
     60   }
     61 
     62   @Implementation
     63   public static synchronized boolean showErrorDialogFragment(int errorCode, Activity activity,
     64       int requestCode) {
     65     return googlePlayServicesUtilImpl.showErrorDialogFragment(
     66         errorCode, activity, requestCode);
     67   }
     68 
     69   @Implementation
     70   public static synchronized boolean showErrorDialogFragment(
     71       int errorCode, Activity activity, int requestCode, OnCancelListener cancelListener) {
     72     return googlePlayServicesUtilImpl.showErrorDialogFragment(
     73         errorCode, activity, requestCode, cancelListener);
     74   }
     75 
     76   @Implementation
     77   public static synchronized Dialog getErrorDialog(int errorCode, Activity activity,
     78       int requestCode) {
     79     return googlePlayServicesUtilImpl.getErrorDialog(errorCode, activity, requestCode);
     80   }
     81 
     82   @Implementation
     83   public static synchronized Dialog getErrorDialog(int errorCode, Activity activity,
     84       int requestCode, OnCancelListener cancelListener) {
     85     return googlePlayServicesUtilImpl.getErrorDialog(
     86         errorCode, activity, requestCode, cancelListener);
     87   }
     88 
     89   @Implementation
     90   public static synchronized PendingIntent getErrorPendingIntent(int errorCode, Context context,
     91       int requestCode) {
     92     return googlePlayServicesUtilImpl.getErrorPendingIntent(errorCode, context, requestCode);
     93   }
     94 
     95   @Implementation
     96   public static synchronized String getOpenSourceSoftwareLicenseInfo(Context context) {
     97     return googlePlayServicesUtilImpl.getOpenSourceSoftwareLicenseInfo(context);
     98   }
     99 
    100   @Implementation
    101   public static synchronized int isGooglePlayServicesAvailable(Context context) {
    102     return googlePlayServicesUtilImpl.isGooglePlayServicesAvailable(context);
    103   }
    104 
    105   @Implementation
    106   public static synchronized void showErrorNotification(int errorCode, Context context) {
    107     googlePlayServicesUtilImpl.showErrorNotification(errorCode, context);
    108   }
    109 
    110   /**
    111    * Class containing methods with same signatures of the static methods of
    112    * {@link GooglePlayServicesUtil}.
    113    */
    114   public static class GooglePlayServicesUtilImpl {
    115     public Dialog getErrorDialog(int errorCode, Activity activity, int requestCode) {
    116       return getErrorDialog(errorCode, activity, requestCode, null);
    117     }
    118 
    119     public Dialog getErrorDialog(int errorCode, Activity activity, int requestCode,
    120         OnCancelListener cancelListener) {
    121       if (errorCode == ConnectionResult.SUCCESS) {
    122         return null;
    123       }
    124       return new Dialog(RuntimeEnvironment.application);
    125     }
    126 
    127     public PendingIntent getErrorPendingIntent(int errorCode, Context context,
    128         int requestCode) {
    129       if (errorCode == ConnectionResult.SUCCESS) {
    130         return null;
    131       }
    132       return PendingIntent.getActivity(
    133           context, requestCode, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
    134     }
    135 
    136     public String getOpenSourceSoftwareLicenseInfo(Context context) {
    137       return "license";
    138     }
    139 
    140     public Context getRemoteContext(Context context) {
    141       return RuntimeEnvironment.application;
    142     }
    143 
    144     public Resources getRemoteResource(Context context) {
    145       return RuntimeEnvironment.application.getResources();
    146     }
    147 
    148     public int isGooglePlayServicesAvailable(Context context) {
    149       return ConnectionResult.SERVICE_MISSING;
    150     }
    151 
    152     public boolean showErrorDialogFragment(int errorCode, Activity activity,
    153         Fragment fragment, int requestCode, OnCancelListener cancelListener) {
    154       return false;
    155     }
    156 
    157     public boolean showErrorDialogFragment(int errorCode, Activity activity, int requestCode) {
    158       return false;
    159     }
    160 
    161     public boolean showErrorDialogFragment(int errorCode, Activity activity, int requestCode,
    162         OnCancelListener cancelListener) {
    163       return false;
    164     }
    165 
    166     public void showErrorNotification(int errorCode, Context context) {}
    167   }
    168 }