mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Create AccountUtil class
This commit is contained in:
parent
a1d8008901
commit
eb58a847b1
8 changed files with 77 additions and 46 deletions
|
|
@ -19,6 +19,7 @@ import com.nostra13.universalimageloader.core.ImageLoader;
|
||||||
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
||||||
import com.nostra13.universalimageloader.utils.StorageUtils;
|
import com.nostra13.universalimageloader.utils.StorageUtils;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.auth.AccountUtil;
|
||||||
import org.acra.ACRA;
|
import org.acra.ACRA;
|
||||||
import org.acra.ReportingInteractionMode;
|
import org.acra.ReportingInteractionMode;
|
||||||
import org.acra.annotation.ReportsCrashes;
|
import org.acra.annotation.ReportsCrashes;
|
||||||
|
|
@ -35,7 +36,6 @@ import org.apache.http.params.CoreProtocolPNames;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import fr.free.nrw.commons.auth.WikiAccountAuthenticator;
|
|
||||||
import fr.free.nrw.commons.caching.CacheController;
|
import fr.free.nrw.commons.caching.CacheController;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
@ -73,6 +73,8 @@ public class CommonsApplication extends Application {
|
||||||
|
|
||||||
public CacheController cacheData;
|
public CacheController cacheData;
|
||||||
|
|
||||||
|
public static CommonsApplication app;
|
||||||
|
|
||||||
public static AbstractHttpClient createHttpClient() {
|
public static AbstractHttpClient createHttpClient() {
|
||||||
BasicHttpParams params = new BasicHttpParams();
|
BasicHttpParams params = new BasicHttpParams();
|
||||||
SchemeRegistry schemeRegistry = new SchemeRegistry();
|
SchemeRegistry schemeRegistry = new SchemeRegistry();
|
||||||
|
|
@ -91,6 +93,7 @@ public class CommonsApplication extends Application {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
app = this;
|
||||||
|
|
||||||
Timber.plant(new Timber.DebugTree());
|
Timber.plant(new Timber.DebugTree());
|
||||||
|
|
||||||
|
|
@ -167,7 +170,7 @@ public class CommonsApplication extends Application {
|
||||||
public Account getCurrentAccount() {
|
public Account getCurrentAccount() {
|
||||||
if(currentAccount == null) {
|
if(currentAccount == null) {
|
||||||
AccountManager accountManager = AccountManager.get(this);
|
AccountManager accountManager = AccountManager.get(this);
|
||||||
Account[] allAccounts = accountManager.getAccountsByType(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
|
||||||
if(allAccounts.length != 0) {
|
if(allAccounts.length != 0) {
|
||||||
currentAccount = allAccounts[0];
|
currentAccount = allAccounts[0];
|
||||||
}
|
}
|
||||||
|
|
@ -183,7 +186,7 @@ public class CommonsApplication extends Application {
|
||||||
return false; // This should never happen
|
return false; // This should never happen
|
||||||
}
|
}
|
||||||
|
|
||||||
accountManager.invalidateAuthToken(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, api.getAuthCookie());
|
accountManager.invalidateAuthToken(AccountUtil.accountType(), api.getAuthCookie());
|
||||||
try {
|
try {
|
||||||
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
|
String authCookie = accountManager.blockingGetAuthToken(curAccount, "", false);
|
||||||
api.setAuthCookie(authCookie);
|
api.setAuthCookie(authCookie);
|
||||||
|
|
|
||||||
61
app/src/main/java/fr/free/nrw/commons/auth/AccountUtil.java
Normal file
61
app/src/main/java/fr/free/nrw/commons/auth/AccountUtil.java
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package fr.free.nrw.commons.auth;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountAuthenticatorResponse;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import fr.free.nrw.commons.CommonsApplication;
|
||||||
|
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
|
||||||
|
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class AccountUtil {
|
||||||
|
|
||||||
|
public static void createAccount(@Nullable AccountAuthenticatorResponse response,
|
||||||
|
String username, String password) {
|
||||||
|
|
||||||
|
Account account = new Account(username, accountType());
|
||||||
|
boolean created = accountManager().addAccountExplicitly(account, password, null);
|
||||||
|
|
||||||
|
Timber.d("account creation " + (created ? "successful" : "failure"));
|
||||||
|
|
||||||
|
if (created) {
|
||||||
|
if (response != null) {
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username);
|
||||||
|
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType());
|
||||||
|
|
||||||
|
|
||||||
|
response.onResult(bundle);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (response != null) {
|
||||||
|
response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "");
|
||||||
|
}
|
||||||
|
Timber.d("account creation failure");
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: If the user turns it off, it shouldn't be auto turned back on
|
||||||
|
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
|
||||||
|
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static String accountType() {
|
||||||
|
return "fr.free.nrw.commons";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AccountManager accountManager() {
|
||||||
|
return AccountManager.get(app());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private static CommonsApplication app() {
|
||||||
|
return CommonsApplication.app;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,8 +20,8 @@ public abstract class AuthenticatedActivity extends BaseActivity {
|
||||||
|
|
||||||
private String authCookie;
|
private String authCookie;
|
||||||
|
|
||||||
public AuthenticatedActivity(String accountType) {
|
public AuthenticatedActivity() {
|
||||||
this.accountType = accountType;
|
this.accountType = AccountUtil.accountType();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GetAuthCookieTask extends AsyncTask<Void, String, String> {
|
private class GetAuthCookieTask extends AsyncTask<Void, String, String> {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
package fr.free.nrw.commons.auth;
|
package fr.free.nrw.commons.auth;
|
||||||
|
|
||||||
import android.accounts.Account;
|
|
||||||
import android.accounts.AccountAuthenticatorResponse;
|
import android.accounts.AccountAuthenticatorResponse;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
@ -12,8 +10,6 @@ import fr.free.nrw.commons.CommonsApplication;
|
||||||
import fr.free.nrw.commons.EventLog;
|
import fr.free.nrw.commons.EventLog;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.contributions.ContributionsActivity;
|
import fr.free.nrw.commons.contributions.ContributionsActivity;
|
||||||
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
|
|
||||||
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
@ -79,33 +75,21 @@ class LoginTask extends AsyncTask<String, String, String> {
|
||||||
private void handlePassResult() {
|
private void handlePassResult() {
|
||||||
loginActivity.showSuccessToastAndDismissDialog();
|
loginActivity.showSuccessToastAndDismissDialog();
|
||||||
|
|
||||||
Account account = new Account(username, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
AccountAuthenticatorResponse response = null;
|
||||||
|
|
||||||
|
|
||||||
boolean accountCreated = AccountManager.get(loginActivity).addAccount(account, password, null);
|
|
||||||
|
|
||||||
|
|
||||||
boolean accountCreated = AccountManager.get(loginActivity).addAccountExplicitly(account, password, null);
|
|
||||||
|
|
||||||
Bundle extras = loginActivity.getIntent().getExtras();
|
Bundle extras = loginActivity.getIntent().getExtras();
|
||||||
|
|
||||||
if (extras != null) {
|
if (extras != null) {
|
||||||
Timber.d("Bundle of extras: %s", extras);
|
Timber.d("Bundle of extras: %s", extras);
|
||||||
if (accountCreated) { // Pass the new account back to the account manager
|
response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
|
||||||
AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
|
if (response != null) {
|
||||||
Bundle authResult = new Bundle();
|
Bundle authResult = new Bundle();
|
||||||
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
|
authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
|
||||||
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountUtil.accountType());
|
||||||
|
|
||||||
if (response != null) {
|
|
||||||
response.onResult(authResult);
|
response.onResult(authResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: If the user turns it off, it shouldn't be auto turned back on
|
AccountUtil.createAccount( response, username, password );
|
||||||
ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
|
|
||||||
ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
|
|
||||||
|
|
||||||
Intent intent = new Intent(loginActivity, ContributionsActivity.class);
|
Intent intent = new Intent(loginActivity, ContributionsActivity.class);
|
||||||
loginActivity.startActivity(intent);
|
loginActivity.startActivity(intent);
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ import fr.free.nrw.commons.MWApi;
|
||||||
|
|
||||||
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
|
|
||||||
public static final String COMMONS_ACCOUNT_TYPE = "fr.free.nrw.commons";
|
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
public WikiAccountAuthenticator(Context context) {
|
public WikiAccountAuthenticator(Context context) {
|
||||||
|
|
@ -38,7 +36,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean supportedAccountType(@Nullable String type) {
|
private boolean supportedAccountType(@Nullable String type) {
|
||||||
return COMMONS_ACCOUNT_TYPE.equals(type);
|
return AccountUtil.accountType().equals(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -104,7 +102,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
if (authCookie != null) {
|
if (authCookie != null) {
|
||||||
final Bundle result = new Bundle();
|
final Bundle result = new Bundle();
|
||||||
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
|
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
|
||||||
result.putString(AccountManager.KEY_ACCOUNT_TYPE, COMMONS_ACCOUNT_TYPE);
|
result.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountUtil.accountType());
|
||||||
result.putString(AccountManager.KEY_AUTHTOKEN, authCookie);
|
result.putString(AccountManager.KEY_AUTHTOKEN, authCookie);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ import fr.free.nrw.commons.HandlerService;
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
||||||
import fr.free.nrw.commons.auth.WikiAccountAuthenticator;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
||||||
import fr.free.nrw.commons.settings.Prefs;
|
import fr.free.nrw.commons.settings.Prefs;
|
||||||
import fr.free.nrw.commons.upload.UploadService;
|
import fr.free.nrw.commons.upload.UploadService;
|
||||||
|
|
@ -63,10 +62,6 @@ public class ContributionsActivity
|
||||||
*/
|
*/
|
||||||
private String CONTRIBUTION_SORT = Contribution.Table.COLUMN_STATE + " DESC, " + Contribution.Table.COLUMN_UPLOADED + " DESC , (" + Contribution.Table.COLUMN_TIMESTAMP + " * " + Contribution.Table.COLUMN_STATE + ")";
|
private String CONTRIBUTION_SORT = Contribution.Table.COLUMN_STATE + " DESC, " + Contribution.Table.COLUMN_UPLOADED + " DESC , (" + Contribution.Table.COLUMN_TIMESTAMP + " * " + Contribution.Table.COLUMN_STATE + ")";
|
||||||
|
|
||||||
public ContributionsActivity() {
|
|
||||||
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServiceConnection uploadServiceConnection = new ServiceConnection() {
|
private ServiceConnection uploadServiceConnection = new ServiceConnection() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import fr.free.nrw.commons.EventLog;
|
||||||
import fr.free.nrw.commons.Media;
|
import fr.free.nrw.commons.Media;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
||||||
import fr.free.nrw.commons.auth.WikiAccountAuthenticator;
|
|
||||||
import fr.free.nrw.commons.category.CategorizationFragment;
|
import fr.free.nrw.commons.category.CategorizationFragment;
|
||||||
import fr.free.nrw.commons.contributions.Contribution;
|
import fr.free.nrw.commons.contributions.Contribution;
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
||||||
|
|
@ -53,10 +52,6 @@ public class MultipleShareActivity
|
||||||
|
|
||||||
private UploadController uploadController;
|
private UploadController uploadController;
|
||||||
|
|
||||||
public MultipleShareActivity() {
|
|
||||||
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Media getMediaAtPosition(int i) {
|
public Media getMediaAtPosition(int i) {
|
||||||
return photosList.get(i);
|
return photosList.get(i);
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ import fr.free.nrw.commons.EventLog;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.Utils;
|
import fr.free.nrw.commons.Utils;
|
||||||
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
import fr.free.nrw.commons.auth.AuthenticatedActivity;
|
||||||
import fr.free.nrw.commons.auth.WikiAccountAuthenticator;
|
|
||||||
import fr.free.nrw.commons.category.CategorizationFragment;
|
import fr.free.nrw.commons.category.CategorizationFragment;
|
||||||
import fr.free.nrw.commons.contributions.Contribution;
|
import fr.free.nrw.commons.contributions.Contribution;
|
||||||
import fr.free.nrw.commons.modifications.CategoryModifier;
|
import fr.free.nrw.commons.modifications.CategoryModifier;
|
||||||
|
|
@ -77,10 +76,6 @@ public class ShareActivity
|
||||||
private String description;
|
private String description;
|
||||||
private Snackbar snackbar;
|
private Snackbar snackbar;
|
||||||
|
|
||||||
public ShareActivity() {
|
|
||||||
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when user taps the submit button
|
* Called when user taps the submit button
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue