Code example for ConnectionResult

Methods: getErrorCode, hasResolution, startResolutionForResult

0
	 
	/** 
	 * Perform resolution given a non-null result. 
	 */ 
	private void resolveLastResult() { 
		if (GooglePlayServicesUtil.isUserRecoverableError(lastConnectionResult.getErrorCode())) {
			// Show a dialog to install or enable Google Play services. 
			showErrorDialog(ErrorDialogFragment.create(lastConnectionResult.getErrorCode(), requestCode));
			return; 
		} 
		 
		if (lastConnectionResult.hasResolution()) {
			startResolution(); 
		} 
	} 
	 
	public static final class ErrorDialogFragment extends GooglePlayServicesErrorDialogFragment { 
		 
		public static ErrorDialogFragment create(int errorCode, int requestCode) {
			ErrorDialogFragment fragment = new ErrorDialogFragment();
			fragment.setArguments(createArguments(errorCode, requestCode));
			return fragment;
		} 
		 
		@Override 
		public void onCancel(DialogInterface dialog) {
			super.onCancel(dialog);
			FragmentActivity activity = getActivity();
			if (activity == null) {
				return; 
			} 
			 
			Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
			if (fragment instanceof PlusClientFragment) {
				((PlusClientFragment)fragment).onDialogCanceled(getTag());
			} 
		} 
		 
		@Override 
		public void onDismiss(DialogInterface dialog) {
			super.onDismiss(dialog);
			FragmentActivity activity = getActivity();
			if (activity == null) {
				return; 
			} 
			 
			Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
			if (fragment instanceof PlusClientFragment) {
				((PlusClientFragment)fragment).onDialogDismissed(getTag());
			} 
		} 
	} 
	 
	private void onDialogCanceled(String tag) {
		requestCode = INVALID_REQUEST_CODE;
		hideProgressDialog(); 
	} 
	 
	private void onDialogDismissed(String tag) {
		if (TAG_PROGRESS_DIALOG.equals(tag)) {
			requestCode = INVALID_REQUEST_CODE;
			hideProgressDialog(); 
		} 
	} 
	 
	private void showProgressDialog() { 
		DialogFragment progressDialog = (DialogFragment)getFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG);
		if (progressDialog == null) {
			progressDialog = ProgressDialogFragment.create();
			progressDialog.show(getFragmentManager(), TAG_PROGRESS_DIALOG);
		} 
	} 
	 
	public static final class ProgressDialogFragment extends DialogFragment {
		 
		private static final String ARG_MESSAGE = "message";
		 
		public static ProgressDialogFragment create(int message) {
			ProgressDialogFragment progressDialogFragment = new ProgressDialogFragment();
			Bundle args = new Bundle();
			args.putInt(ARG_MESSAGE, message);
			progressDialogFragment.setArguments(args);
			return progressDialogFragment;
		} 
		 
		public static ProgressDialogFragment create() { 
			return create(R.string.signingToGooglePlus); 
		} 
		 
		@Override 
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			ProgressDialog progressDialog = new ProgressDialog(getActivity());
			progressDialog.setIndeterminate(true);
			progressDialog.setMessage(getString(getArguments().getInt(ARG_MESSAGE)));
			return progressDialog;
		} 
		 
		@Override 
		public void onCancel(DialogInterface dialog) {
			super.onCancel(dialog);
			FragmentActivity activity = getActivity();
			if (activity == null) {
				return; 
			} 
			 
			Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
			if (fragment instanceof PlusClientFragment) {
				((PlusClientFragment)fragment).onDialogCanceled(getTag());
			} 
		} 
		 
		@Override 
		public void onDismiss(DialogInterface dialog) {
			super.onDismiss(dialog);
			FragmentActivity activity = getActivity();
			if (activity == null) {
				return; 
			} 
			 
			Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(TAG_PLUS_CLIENT);
			if (fragment instanceof PlusClientFragment) {
				((PlusClientFragment)fragment).onDialogDismissed(getTag());
			} 
		} 
	} 
	 
	protected void hideProgressDialog() { 
		FragmentManager manager = getFragmentManager();
		if (manager != null) {
			DialogFragment progressDialog = (DialogFragment)manager.findFragmentByTag(TAG_PROGRESS_DIALOG);
			if (progressDialog != null) {
				progressDialog.dismiss();
			} 
		} 
	} 
	 
	private void showErrorDialog(DialogFragment errorDialog) {
		DialogFragment oldErrorDialog = (DialogFragment)getFragmentManager().findFragmentByTag(TAG_ERROR_DIALOG);
		if (oldErrorDialog != null) {
			oldErrorDialog.dismiss();
		} 
		 
		errorDialog.show(getFragmentManager(), TAG_ERROR_DIALOG);
	} 
	 
	private boolean isShowingErrorDialog() { 
		DialogFragment errorDialog = (DialogFragment)getFragmentManager().findFragmentByTag(TAG_ERROR_DIALOG);
		return (errorDialog != null) && !errorDialog.isHidden();
	} 
	 
	private void hideErrorDialog() { 
		DialogFragment errorDialog = (DialogFragment)getFragmentManager().findFragmentByTag(TAG_ERROR_DIALOG);
		if (errorDialog != null) {
			errorDialog.dismiss();
		} 
	} 
	 
	private void startResolution() { 
		try { 
			lastConnectionResult.startResolutionForResult(getActivity(), requestCode);
			hideProgressDialog(); 
		} catch (SendIntentException e) {
			// The intent we had is not valid right now, perhaps the remote process died. 
			// Try to reconnect to get a new resolution intent. 
			lastConnectionResult = null;