mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Move checking for service permission at AccountChooserActivity.
This commit is contained in:
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks.io.docs;
|
||||
|
||||
import com.google.android.apps.mytracks.io.gdata.docs.DocumentsClient;
|
||||
import com.google.android.apps.mytracks.io.gdata.docs.SpreadsheetsClient;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendActivity;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
|
||||
@@ -32,42 +30,6 @@ import android.content.Intent;
|
||||
*/
|
||||
public class SendDocsActivity extends AbstractSendActivity {
|
||||
|
||||
private static final String TAG = SendDocsActivity.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
protected String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAuthTokenType() {
|
||||
return DocumentsClient.SERVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PermissionCallback getPermissionCallback() {
|
||||
return new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
// prompts for permission to access Google spreadsheet
|
||||
promptPermission(SpreadsheetsClient.SERVICE, new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
executeAsyncTask();
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
startNextActivity(false, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
startNextActivity(false, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractSendAsyncTask createAsyncTask() {
|
||||
return new SendDocsAsyncTask(this, sendRequest.getTrackId(), sendRequest.getAccount());
|
||||
|
||||
-26
@@ -31,32 +31,6 @@ import android.content.Intent;
|
||||
*/
|
||||
public class SendFusionTablesActivity extends AbstractSendActivity {
|
||||
|
||||
private static final String TAG = SendFusionTablesActivity.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
protected String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAuthTokenType() {
|
||||
return SendFusionTablesUtils.SERVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PermissionCallback getPermissionCallback() {
|
||||
return new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
executeAsyncTask();
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
startNextActivity(false, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractSendAsyncTask createAsyncTask() {
|
||||
return new SendFusionTablesAsyncTask(this, sendRequest.getTrackId(), sendRequest.getAccount());
|
||||
|
||||
@@ -15,16 +15,10 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks.io.maps;
|
||||
|
||||
import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
|
||||
import com.google.android.apps.mytracks.io.gdata.maps.MapsMapMetadata;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
@@ -35,7 +29,6 @@ import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
@@ -43,7 +36,6 @@ import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@@ -55,7 +47,6 @@ public class ChooseMapActivity extends Activity {
|
||||
|
||||
private static final int PROGRESS_DIALOG = 1;
|
||||
private static final int ERROR_DIALOG = 2;
|
||||
private static final String TAG = ChooseMapActivity.class.getSimpleName();
|
||||
|
||||
private SendRequest sendRequest;
|
||||
private ChooseMapAsyncTask asyncTask;
|
||||
@@ -114,46 +105,13 @@ public class ChooseMapActivity extends Activity {
|
||||
asyncTask = (ChooseMapAsyncTask) retained;
|
||||
asyncTask.setActivity(this);
|
||||
} else {
|
||||
promptPermission();
|
||||
asyncTask = new ChooseMapAsyncTask(this, sendRequest.getAccount());
|
||||
asyncTask.execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompts the user for permission to access the Google Maps service.
|
||||
*/
|
||||
private void promptPermission() {
|
||||
AccountManager.get(this).getAuthToken(sendRequest.getAccount(), MapsConstants.SERVICE_NAME,
|
||||
null, this, new AccountManagerCallback<Bundle>() {
|
||||
@Override
|
||||
public void run(AccountManagerFuture<Bundle> future) {
|
||||
try {
|
||||
if (future.getResult().getString(AccountManager.KEY_AUTHTOKEN) != null) {
|
||||
asyncTask = new ChooseMapAsyncTask(
|
||||
ChooseMapActivity.this, sendRequest.getAccount());
|
||||
asyncTask.execute();
|
||||
} else {
|
||||
Log.d(TAG, "auth token is null");
|
||||
finish();
|
||||
}
|
||||
} catch (OperationCanceledException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
finish();
|
||||
} catch (AuthenticatorException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
finish();
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object onRetainNonConfigurationInstance() {
|
||||
if (asyncTask == null) {
|
||||
return null;
|
||||
}
|
||||
asyncTask.setActivity(null);
|
||||
return asyncTask;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ package com.google.android.apps.mytracks.io.maps;
|
||||
|
||||
import com.google.android.apps.mytracks.io.docs.SendDocsActivity;
|
||||
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity;
|
||||
import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendActivity;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
|
||||
@@ -33,31 +32,6 @@ import android.content.Intent;
|
||||
*/
|
||||
public class SendMapsActivity extends AbstractSendActivity {
|
||||
|
||||
private static final String TAG = SendMapsActivity.class.getSimpleName();
|
||||
|
||||
protected String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAuthTokenType() {
|
||||
return MapsConstants.SERVICE_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PermissionCallback getPermissionCallback() {
|
||||
return new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
executeAsyncTask();
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
startNextActivity(false, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractSendAsyncTask createAsyncTask() {
|
||||
return new SendMapsAsyncTask(
|
||||
|
||||
+2
-87
@@ -18,19 +18,11 @@ package com.google.android.apps.mytracks.io.sendtogoogle;
|
||||
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* The abstract class for activities sending a track to Google.
|
||||
@@ -51,25 +43,6 @@ public abstract class AbstractSendActivity extends Activity {
|
||||
|
||||
private static final int PROGRESS_DIALOG = 1;
|
||||
|
||||
/**
|
||||
* A callback after prompting the user for permission to access a Google
|
||||
* service.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public interface PermissionCallback {
|
||||
|
||||
/**
|
||||
* Invoke when the permission is granted.
|
||||
*/
|
||||
public void onSuccess();
|
||||
|
||||
/**
|
||||
* Invoke when the permission is not granted.
|
||||
*/
|
||||
public void onFailure();
|
||||
}
|
||||
|
||||
protected SendRequest sendRequest;
|
||||
private AbstractSendAsyncTask asyncTask;
|
||||
private ProgressDialog progressDialog;
|
||||
@@ -84,15 +57,13 @@ public abstract class AbstractSendActivity extends Activity {
|
||||
asyncTask = (AbstractSendAsyncTask) retained;
|
||||
asyncTask.setActivity(this);
|
||||
} else {
|
||||
promptPermission(getAuthTokenType(), getPermissionCallback());
|
||||
asyncTask = createAsyncTask();
|
||||
asyncTask.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object onRetainNonConfigurationInstance() {
|
||||
if (asyncTask == null) {
|
||||
return null;
|
||||
}
|
||||
asyncTask.setActivity(null);
|
||||
return asyncTask;
|
||||
}
|
||||
@@ -148,62 +119,6 @@ public abstract class AbstractSendActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logging TAG.
|
||||
*/
|
||||
protected abstract String getTag();
|
||||
|
||||
/**
|
||||
* Gets the auth token type.
|
||||
*/
|
||||
protected abstract String getAuthTokenType();
|
||||
|
||||
/**
|
||||
* Gets the callback for requesting permission to access a service.
|
||||
*/
|
||||
protected abstract PermissionCallback getPermissionCallback();
|
||||
|
||||
/**
|
||||
* Prompts the user for permission to access the service.
|
||||
*
|
||||
* @param authTokenType the auth token type
|
||||
* @param callback the callback
|
||||
*/
|
||||
protected void promptPermission(String authTokenType, final PermissionCallback callback) {
|
||||
AccountManager.get(this).getAuthToken(
|
||||
sendRequest.getAccount(), authTokenType, null, this, new AccountManagerCallback<Bundle>() {
|
||||
@Override
|
||||
public void run(AccountManagerFuture<Bundle> future) {
|
||||
try {
|
||||
if (future.getResult().getString(AccountManager.KEY_AUTHTOKEN) != null) {
|
||||
callback.onSuccess();
|
||||
} else {
|
||||
Log.d(getTag(), "auth token is null");
|
||||
callback.onFailure();
|
||||
}
|
||||
} catch (OperationCanceledException e) {
|
||||
Log.d(getTag(), "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
} catch (AuthenticatorException e) {
|
||||
Log.d(getTag(), "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
} catch (IOException e) {
|
||||
Log.d(getTag(), "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the AsyncTask after obtaining user permission to access the
|
||||
* service.
|
||||
*/
|
||||
protected void executeAsyncTask() {
|
||||
asyncTask = createAsyncTask();
|
||||
asyncTask.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the AsyncTask.
|
||||
*/
|
||||
|
||||
+120
-6
@@ -18,6 +18,10 @@ package com.google.android.apps.mytracks.io.sendtogoogle;
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.io.docs.SendDocsActivity;
|
||||
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity;
|
||||
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesUtils;
|
||||
import com.google.android.apps.mytracks.io.gdata.docs.DocumentsClient;
|
||||
import com.google.android.apps.mytracks.io.gdata.docs.SpreadsheetsClient;
|
||||
import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
|
||||
import com.google.android.apps.mytracks.io.maps.ChooseMapActivity;
|
||||
import com.google.android.apps.mytracks.io.maps.SendMapsActivity;
|
||||
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
|
||||
@@ -25,6 +29,10 @@ import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
@@ -34,6 +42,9 @@ import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A chooser to select an account.
|
||||
@@ -42,9 +53,29 @@ import android.os.Bundle;
|
||||
*/
|
||||
public class AccountChooserActivity extends Activity {
|
||||
|
||||
private static final String TAG = AccountChooserActivity.class.getSimpleName();
|
||||
|
||||
private static final int NO_ACCOUNT_DIALOG = 1;
|
||||
private static final int CHOOSE_ACCOUNT_DIALOG = 2;
|
||||
|
||||
/**
|
||||
* A callback after getting the permission to access a Google service.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
private interface PermissionCallback {
|
||||
|
||||
/**
|
||||
* Invoke when success.
|
||||
*/
|
||||
public void onSuccess();
|
||||
|
||||
/**
|
||||
* Invoke when failed.
|
||||
*/
|
||||
public void onFailure();
|
||||
}
|
||||
|
||||
private SendRequest sendRequest;
|
||||
private Account[] accounts;
|
||||
private int selectedAccountIndex;
|
||||
@@ -56,7 +87,8 @@ public class AccountChooserActivity extends Activity {
|
||||
accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
|
||||
|
||||
if (accounts.length == 1) {
|
||||
startNextActivity(accounts[0]);
|
||||
sendRequest.setAccount(accounts[0]);
|
||||
getPermission(MapsConstants.SERVICE_NAME, sendRequest.isSendMaps(), mapsCallback);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -138,7 +170,9 @@ public class AccountChooserActivity extends Activity {
|
||||
Editor editor = prefs.edit();
|
||||
editor.putString(getString(R.string.preferred_account_key), account.name);
|
||||
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
|
||||
startNextActivity(account);
|
||||
|
||||
sendRequest.setAccount(account);
|
||||
getPermission(MapsConstants.SERVICE_NAME, sendRequest.isSendMaps(), mapsCallback);
|
||||
}
|
||||
});
|
||||
return builder.create();
|
||||
@@ -146,6 +180,89 @@ public class AccountChooserActivity extends Activity {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private PermissionCallback spreadsheetsCallback = new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
startNextActivity();
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
private PermissionCallback docsCallback = new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
getPermission(SpreadsheetsClient.SERVICE, sendRequest.isSendDocs(), spreadsheetsCallback);
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
private PermissionCallback fusionTablesCallback = new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
getPermission(DocumentsClient.SERVICE, sendRequest.isSendDocs(), docsCallback);
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
private PermissionCallback mapsCallback = new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
getPermission(
|
||||
SendFusionTablesUtils.SERVICE, sendRequest.isSendFusionTables(), fusionTablesCallback);
|
||||
}
|
||||
@Override
|
||||
public void onFailure() {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the user permission to access a service.
|
||||
*
|
||||
* @param authTokenType the auth token type of the service
|
||||
* @param needPermission true if need the permission
|
||||
* @param callback callback after getting the permission
|
||||
*/
|
||||
private void getPermission(
|
||||
String authTokenType, boolean needPermission, final PermissionCallback callback) {
|
||||
if (needPermission) {
|
||||
AccountManager.get(this).getAuthToken(sendRequest.getAccount(), authTokenType, null, this,
|
||||
new AccountManagerCallback<Bundle>() {
|
||||
@Override
|
||||
public void run(AccountManagerFuture<Bundle> future) {
|
||||
try {
|
||||
if (future.getResult().getString(AccountManager.KEY_AUTHTOKEN) != null) {
|
||||
callback.onSuccess();
|
||||
} else {
|
||||
Log.d(TAG, "auth token is null");
|
||||
callback.onFailure();
|
||||
}
|
||||
} catch (OperationCanceledException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
} catch (AuthenticatorException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, "Unable to get auth token", e);
|
||||
callback.onFailure();
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
} else {
|
||||
callback.onSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the next activity. If
|
||||
@@ -160,11 +277,8 @@ public class AccountChooserActivity extends Activity {
|
||||
* <p>
|
||||
* !sendMaps && !sendFusionTables && !sendDocs -> {@link UploadResultActivity}
|
||||
*
|
||||
* @param account the chosen account
|
||||
*/
|
||||
private void startNextActivity(Account account) {
|
||||
sendRequest.setAccount(account);
|
||||
|
||||
private void startNextActivity() {
|
||||
Class<?> next;
|
||||
if (sendRequest.isSendMaps()) {
|
||||
next = sendRequest.isNewMap() ? SendMapsActivity.class : ChooseMapActivity.class;
|
||||
|
||||
Reference in New Issue
Block a user