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