forked from upstream-mirrors/OpenTracks
Use CheckPermissionAsyncTask in GoogleSettingsActivity
This commit is contained in:
@@ -92,7 +92,7 @@ limitations under the License.
|
||||
<uses-permission android:name="com.google.android.maps.mytracks.permission.MAPS_RECEIVE" />
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="17" />
|
||||
android:targetSdkVersion="19" />
|
||||
<!-- Required features -->
|
||||
<uses-feature
|
||||
android:glEsVersion="0x00020000"
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.google.android.apps.mytracks.io.spreadsheets.SendSpreadsheetsActivity
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
|
||||
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask;
|
||||
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask.CheckPermissionCaller;
|
||||
import com.google.android.apps.mytracks.util.AnalyticsUtils;
|
||||
import com.google.android.apps.mytracks.util.GoogleEarthUtils;
|
||||
import com.google.android.apps.mytracks.util.IntentUtils;
|
||||
@@ -68,8 +69,8 @@ import java.io.IOException;
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActivity implements
|
||||
ChooseAccountCaller, ConfirmSyncCaller, ShareTrackCaller, ConfirmDeleteCaller,
|
||||
PlayMultipleCaller {
|
||||
ChooseAccountCaller, ConfirmSyncCaller, CheckPermissionCaller, ShareTrackCaller,
|
||||
ConfirmDeleteCaller, PlayMultipleCaller {
|
||||
|
||||
private static final String TAG = AbstractMyTracksActivity.class.getSimpleName();
|
||||
private static final String SEND_REQUEST_KEY = "send_request_key";
|
||||
@@ -215,6 +216,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
|
||||
asyncTask.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckPermissionDone(String scope, boolean success, Intent userRecoverableIntent) {
|
||||
asyncTask = null;
|
||||
if (success) {
|
||||
|
||||
+12
-7
@@ -16,12 +16,12 @@
|
||||
|
||||
package com.google.android.apps.mytracks.services.tasks;
|
||||
|
||||
import com.google.android.apps.mytracks.AbstractSendToGoogleActivity;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.gms.auth.GoogleAuthException;
|
||||
import com.google.android.gms.auth.UserRecoverableAuthException;
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
@@ -35,9 +35,13 @@ import java.io.IOException;
|
||||
*/
|
||||
public class CheckPermissionAsyncTask extends AsyncTask<Void, Void, Boolean> {
|
||||
|
||||
public interface CheckPermissionCaller {
|
||||
public void onCheckPermissionDone(String scope, boolean success, Intent userRecoverableIntent);
|
||||
}
|
||||
|
||||
private static final String TAG = CheckPermissionAsyncTask.class.getSimpleName();
|
||||
|
||||
private AbstractSendToGoogleActivity activity;
|
||||
private Activity activity;
|
||||
private final String accountName;
|
||||
private final String scope;
|
||||
|
||||
@@ -61,8 +65,7 @@ public class CheckPermissionAsyncTask extends AsyncTask<Void, Void, Boolean> {
|
||||
*/
|
||||
private boolean canRetry;
|
||||
|
||||
public CheckPermissionAsyncTask(
|
||||
AbstractSendToGoogleActivity activity, String accountName, String scope) {
|
||||
public CheckPermissionAsyncTask(Activity activity, String accountName, String scope) {
|
||||
this.activity = activity;
|
||||
this.accountName = accountName;
|
||||
this.scope = scope;
|
||||
@@ -72,10 +75,11 @@ public class CheckPermissionAsyncTask extends AsyncTask<Void, Void, Boolean> {
|
||||
canRetry = true;
|
||||
}
|
||||
|
||||
public void setActivity(AbstractSendToGoogleActivity activity) {
|
||||
public void setActivity(Activity activity) {
|
||||
this.activity = activity;
|
||||
if (completed && activity != null) {
|
||||
activity.onCheckPermissionDone(scope, success, userRecoverableIntent);
|
||||
((CheckPermissionCaller) activity).onCheckPermissionDone(
|
||||
scope, success, userRecoverableIntent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +93,8 @@ public class CheckPermissionAsyncTask extends AsyncTask<Void, Void, Boolean> {
|
||||
success = result;
|
||||
completed = true;
|
||||
if (activity != null) {
|
||||
activity.onCheckPermissionDone(scope, success, userRecoverableIntent);
|
||||
((CheckPermissionCaller) activity).onCheckPermissionDone(
|
||||
scope, success, userRecoverableIntent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+40
-51
@@ -19,12 +19,11 @@ package com.google.android.apps.mytracks.settings;
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask;
|
||||
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask.CheckPermissionCaller;
|
||||
import com.google.android.apps.mytracks.util.DialogUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.gms.auth.GoogleAuthException;
|
||||
import com.google.android.gms.auth.UserRecoverableAuthException;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
@@ -39,10 +38,8 @@ import android.preference.CheckBoxPreference;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -51,9 +48,9 @@ import java.util.List;
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
public class GoogleSettingsActivity extends AbstractSettingsActivity
|
||||
implements CheckPermissionCaller {
|
||||
|
||||
private static final String TAG = GoogleSettingsActivity.class.getSimpleName();
|
||||
private static final String ACCOUNT_NAME_KEY = "accountName";
|
||||
private static final int DIALOG_CONFIRM_SWITCH_ACCOUNT = 0;
|
||||
private static final int DIALOG_CONFIRM_DRIVE_SYNC_ON = 1;
|
||||
@@ -61,10 +58,19 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
private ListPreference googleAccountPreference;
|
||||
private CheckBoxPreference driveSyncPreference;
|
||||
|
||||
private CheckPermissionAsyncTask asyncTask;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
|
||||
Object retained = getLastNonConfigurationInstance();
|
||||
if (retained instanceof CheckPermissionAsyncTask) {
|
||||
asyncTask = (CheckPermissionAsyncTask) retained;
|
||||
asyncTask.setActivity(this);
|
||||
}
|
||||
|
||||
addPreferencesFromResource(R.xml.google_settings);
|
||||
|
||||
googleAccountPreference = (ListPreference) findPreference(
|
||||
@@ -119,6 +125,14 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Object onRetainNonConfigurationInstance() {
|
||||
if (asyncTask != null) {
|
||||
asyncTask.setActivity(null);
|
||||
}
|
||||
return asyncTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
@@ -149,36 +163,14 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
getString(R.string.sync_drive_confirm_message), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
final String googleAccount = PreferencesUtils.getString(GoogleSettingsActivity.this,
|
||||
R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
/*
|
||||
* This class, a PreferenceActivity, needs to support api level
|
||||
* 8+, thus cannot use CheckPermissionFragment because a
|
||||
* Fragment is only available for api level 11+ and there is no
|
||||
* support library for PreferenceActivity.
|
||||
*/
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
SendToGoogleUtils.getGoogleAccountCredential(
|
||||
GoogleSettingsActivity.this, googleAccount,
|
||||
SendToGoogleUtils.DRIVE_SCOPE);
|
||||
handleDriveAccess(true, null);
|
||||
} catch (UserRecoverableAuthException e) {
|
||||
handleDriveAccess(false, e.getIntent());
|
||||
} catch (GoogleAuthException e) {
|
||||
Log.e(TAG, "GoogleAuthException", e);
|
||||
handleDriveAccess(false, null);
|
||||
} catch (UserRecoverableAuthIOException e) {
|
||||
handleDriveAccess(false, e.getIntent());
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IOException", e);
|
||||
handleDriveAccess(false, null);
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
if (asyncTask == null) {
|
||||
final String googleAccount = PreferencesUtils.getString(
|
||||
GoogleSettingsActivity.this, R.string.google_account_key,
|
||||
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
asyncTask = new CheckPermissionAsyncTask(
|
||||
GoogleSettingsActivity.this, googleAccount, SendToGoogleUtils.DRIVE_SCOPE);
|
||||
asyncTask.execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
@@ -188,21 +180,18 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
return dialog;
|
||||
}
|
||||
|
||||
private void handleDriveAccess(final boolean success, final Intent intent) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (success) {
|
||||
onDrivePermissionSuccess();
|
||||
} else {
|
||||
if (intent != null) {
|
||||
startActivityForResult(intent, DRIVE_REQUEST_CODE);
|
||||
} else {
|
||||
onDrivePermissionFailure();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onCheckPermissionDone(String scope, boolean success, Intent userRecoverableIntent) {
|
||||
asyncTask = null;
|
||||
if (success) {
|
||||
onDrivePermissionSuccess();
|
||||
} else {
|
||||
if (userRecoverableIntent != null) {
|
||||
startActivityForResult(userRecoverableIntent, DRIVE_REQUEST_CODE);
|
||||
} else {
|
||||
onDrivePermissionFailure();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onDrivePermissionSuccess() {
|
||||
|
||||
Reference in New Issue
Block a user