11package org .thoughtcrime .securesms ;
22
33import android .content .ActivityNotFoundException ;
4+ import android .content .Context ;
45import android .content .DialogInterface ;
56import android .content .Intent ;
67import android .net .Uri ;
2021import android .widget .Toast ;
2122
2223import com .google .android .gms .common .ConnectionResult ;
24+ import com .google .android .gms .common .GoogleApiAvailability ;
2325import com .google .android .gms .common .GooglePlayServicesUtil ;
2426import com .google .i18n .phonenumbers .AsYouTypeFormatter ;
2527import com .google .i18n .phonenumbers .NumberParseException ;
@@ -44,6 +46,13 @@ public class RegistrationActivity extends BaseActionBarActivity {
4446 private static final int PICK_COUNTRY = 1 ;
4547 private static final String TAG = RegistrationActivity .class .getSimpleName ();
4648
49+ private enum PlayServicesStatus {
50+ SUCCESS ,
51+ MISSING ,
52+ NEEDS_UPDATE ,
53+ TRANSIENT_ERROR
54+ }
55+
4756 private AsYouTypeFormatter countryFormatter ;
4857 private ArrayAdapter <String > countrySpinnerAdapter ;
4958 private Spinner countrySpinner ;
@@ -211,35 +220,81 @@ public void onClick(View v) {
211220 return ;
212221 }
213222
214- int gcmStatus = GooglePlayServicesUtil . isGooglePlayServicesAvailable (self );
223+ PlayServicesStatus gcmStatus = checkPlayServices (self );
215224
216- if (gcmStatus != ConnectionResult .SUCCESS ) {
217- if (GooglePlayServicesUtil .isUserRecoverableError (gcmStatus )) {
218- GooglePlayServicesUtil .getErrorDialog (gcmStatus , self , 9000 ).show ();
219- } else {
220- Dialogs .showAlertDialog (self , getString (R .string .RegistrationActivity_unsupported ),
221- getString (R .string .RegistrationActivity_sorry_this_device_is_not_supported_for_data_messaging ));
222- }
223- return ;
225+ if (gcmStatus == PlayServicesStatus .SUCCESS ) {
226+ promptForRegistrationStart (self , e164number , true );
227+ } else if (gcmStatus == PlayServicesStatus .MISSING ) {
228+ promptForNoPlayServices (self , e164number );
229+ } else if (gcmStatus == PlayServicesStatus .NEEDS_UPDATE ) {
230+ GoogleApiAvailability .getInstance ().getErrorDialog (self , ConnectionResult .SERVICE_VERSION_UPDATE_REQUIRED , 0 );
231+ } else {
232+ Dialogs .showAlertDialog (self , getString (R .string .RegistrationActivity_play_services_error ),
233+ getString (R .string .RegistrationActivity_google_play_services_is_updating_or_unavailable ));
224234 }
235+ }
225236
226- AlertDialog .Builder dialog = new AlertDialog .Builder (self );
237+ private void promptForRegistrationStart (final Context context , final String e164number , final boolean gcmSupported ) {
238+ AlertDialog .Builder dialog = new AlertDialog .Builder (context );
227239 dialog .setTitle (PhoneNumberFormatter .getInternationalFormatFromE164 (e164number ));
228240 dialog .setMessage (R .string .RegistrationActivity_we_will_now_verify_that_the_following_number_is_associated_with_your_device_s );
229241 dialog .setPositiveButton (getString (R .string .RegistrationActivity_continue ),
230242 new DialogInterface .OnClickListener () {
231243 @ Override
232244 public void onClick (DialogInterface dialog , int which ) {
233- Intent intent = new Intent (self , RegistrationProgressActivity .class );
234- intent .putExtra ("e164number" , e164number );
235- intent .putExtra ("master_secret" , masterSecret );
245+ Intent intent = new Intent (context , RegistrationProgressActivity .class );
246+ intent .putExtra (RegistrationProgressActivity .NUMBER_EXTRA , e164number );
247+ intent .putExtra (RegistrationProgressActivity .MASTER_SECRET_EXTRA , masterSecret );
248+ intent .putExtra (RegistrationProgressActivity .GCM_SUPPORTED_EXTRA , gcmSupported );
236249 startActivity (intent );
237250 finish ();
238251 }
239252 });
240253 dialog .setNegativeButton (getString (R .string .RegistrationActivity_edit ), null );
241254 dialog .show ();
242255 }
256+
257+ private void promptForNoPlayServices (final Context context , final String e164number ) {
258+ AlertDialog .Builder dialog = new AlertDialog .Builder (context );
259+ dialog .setTitle (R .string .RegistrationActivity_missing_google_play_services );
260+ dialog .setMessage (R .string .RegistrationActivity_this_device_is_missing_google_play_services );
261+ dialog .setPositiveButton (R .string .RegistrationActivity_i_understand , new DialogInterface .OnClickListener () {
262+ @ Override
263+ public void onClick (DialogInterface dialog , int which ) {
264+ promptForRegistrationStart (context , e164number , false );
265+ }
266+ });
267+ dialog .setNegativeButton (android .R .string .cancel , null );
268+ dialog .show ();
269+ }
270+
271+ private PlayServicesStatus checkPlayServices (Context context ) {
272+ int gcmStatus = 0 ;
273+
274+ try {
275+ gcmStatus = GoogleApiAvailability .getInstance ().isGooglePlayServicesAvailable (context );
276+ } catch (Throwable t ) {
277+ Log .w (TAG , t );
278+ return PlayServicesStatus .MISSING ;
279+ }
280+
281+ Log .w (TAG , "Play Services: " + gcmStatus );
282+
283+ switch (gcmStatus ) {
284+ case ConnectionResult .SUCCESS :
285+ return PlayServicesStatus .SUCCESS ;
286+ case ConnectionResult .SERVICE_VERSION_UPDATE_REQUIRED :
287+ return PlayServicesStatus .NEEDS_UPDATE ;
288+ case ConnectionResult .SERVICE_DISABLED :
289+ case ConnectionResult .SERVICE_MISSING :
290+ case ConnectionResult .SERVICE_INVALID :
291+ case ConnectionResult .API_UNAVAILABLE :
292+ case ConnectionResult .SERVICE_MISSING_PERMISSION :
293+ return PlayServicesStatus .MISSING ;
294+ default :
295+ return PlayServicesStatus .TRANSIENT_ERROR ;
296+ }
297+ }
243298 }
244299
245300 private class CountryCodeChangedListener implements TextWatcher {
0 commit comments