forked from upstream-mirrors/OpenTracks
Code cleanup. Remove PermissionCallback and remove checkPermission in GoogleSettingsActivity.
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013 Google Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
||||||
* use this file except in compliance with the License. You may obtain a copy of
|
|
||||||
* the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations under
|
|
||||||
* the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.android.apps.mytracks.io.sendtogoogle;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A callback after getting the permission to access a Google service.
|
|
||||||
*
|
|
||||||
* @author Jimmy Shih
|
|
||||||
*/
|
|
||||||
public interface PermissionCallback {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* To be invoked when the permission is granted.
|
|
||||||
*/
|
|
||||||
public void onSuccess();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* To be invoked when the permission is not granted.
|
|
||||||
*/
|
|
||||||
public void onFailure();
|
|
||||||
}
|
|
||||||
@@ -20,15 +20,12 @@ import com.google.android.apps.mytracks.content.Track;
|
|||||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||||
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.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
||||||
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
|
|
||||||
import com.google.api.services.drive.DriveScopes;
|
import com.google.api.services.drive.DriveScopes;
|
||||||
import com.google.api.services.fusiontables.FusiontablesScopes;
|
import com.google.api.services.fusiontables.FusiontablesScopes;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -174,55 +171,6 @@ public class SendToGoogleUtils {
|
|||||||
return credential.getToken();
|
return credential.getToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks permission by an activity. Will start an activity to request
|
|
||||||
* permission using the request code.
|
|
||||||
*
|
|
||||||
* @param activity the activity
|
|
||||||
* @param accountName the account name
|
|
||||||
* @param scope the scope
|
|
||||||
* @param requestCode the request code
|
|
||||||
* @param permissionCallback the permission callback
|
|
||||||
*/
|
|
||||||
public static void checkPermissionByActivity(final Activity activity, final String accountName,
|
|
||||||
final String scope, final int requestCode, final PermissionCallback permissionCallback) {
|
|
||||||
Thread thread = new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
getGoogleAccountCredential(activity, accountName, scope);
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
permissionCallback.onSuccess();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (UserRecoverableAuthException e) {
|
|
||||||
activity.startActivityForResult(e.getIntent(), requestCode);
|
|
||||||
} catch (GoogleAuthException e) {
|
|
||||||
Log.e(TAG, "GoogleAuthException", e);
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
permissionCallback.onFailure();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (UserRecoverableAuthIOException e) {
|
|
||||||
activity.startActivityForResult(e.getIntent(), requestCode);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "IOException", e);
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
permissionCallback.onFailure();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
thread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a notification to request permission.
|
* Sends a notification to request permission.
|
||||||
*
|
*
|
||||||
|
|||||||
+64
-21
@@ -17,12 +17,14 @@
|
|||||||
package com.google.android.apps.mytracks.settings;
|
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.PermissionCallback;
|
|
||||||
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.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;
|
||||||
@@ -37,8 +39,10 @@ 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;
|
||||||
|
|
||||||
@@ -49,24 +53,11 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
private PermissionCallback permissionCallback = new PermissionCallback() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess() {
|
|
||||||
handleSync(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure() {
|
|
||||||
Toast.makeText(
|
|
||||||
GoogleSettingsActivity.this, R.string.send_google_no_account_message, Toast.LENGTH_LONG)
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private ListPreference googleAccountPreference;
|
private ListPreference googleAccountPreference;
|
||||||
private CheckBoxPreference driveSyncPreference;
|
private CheckBoxPreference driveSyncPreference;
|
||||||
|
|
||||||
@@ -143,9 +134,9 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
|||||||
case SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE:
|
case SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE:
|
||||||
SendToGoogleUtils.cancelNotification(this, SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
|
SendToGoogleUtils.cancelNotification(this, SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
permissionCallback.onSuccess();
|
onDrivePermissionSuccess();
|
||||||
} else {
|
} else {
|
||||||
permissionCallback.onFailure();
|
onDrivePermissionFailure();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -167,12 +158,37 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
|||||||
new DialogInterface.OnClickListener() {
|
new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface d, int button) {
|
public void onClick(DialogInterface d, int button) {
|
||||||
String googleAccount = PreferencesUtils.getString(
|
final String googleAccount = PreferencesUtils.getString(
|
||||||
GoogleSettingsActivity.this, R.string.google_account_key,
|
GoogleSettingsActivity.this, R.string.google_account_key,
|
||||||
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||||
SendToGoogleUtils.checkPermissionByActivity(GoogleSettingsActivity.this,
|
/*
|
||||||
googleAccount, SendToGoogleUtils.DRIVE_SCOPE,
|
* This class, a PreferenceActivity, needs to support api level
|
||||||
SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE, permissionCallback);
|
* 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();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@@ -182,6 +198,33 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
|||||||
return dialog;
|
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, SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE);
|
||||||
|
} else {
|
||||||
|
onDrivePermissionFailure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onDrivePermissionSuccess() {
|
||||||
|
handleSync(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onDrivePermissionFailure() {
|
||||||
|
Toast.makeText(
|
||||||
|
GoogleSettingsActivity.this, R.string.send_google_no_account_message, Toast.LENGTH_LONG)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
|
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
|
||||||
AlertDialog alertDialog = (AlertDialog) dialog;
|
AlertDialog alertDialog = (AlertDialog) dialog;
|
||||||
|
|||||||
Reference in New Issue
Block a user