mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Properly retreive account from AccountManager for auth
This commit is contained in:
parent
e3a2384956
commit
76fc431532
5 changed files with 176 additions and 31 deletions
|
|
@ -10,7 +10,8 @@
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
|
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
|
||||||
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
|
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
|
||||||
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
|
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
|
||||||
|
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".CommonsApplication"
|
android:name=".CommonsApplication"
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import java.io.*;
|
||||||
|
|
||||||
import org.mediawiki.api.ApiResult;
|
import org.mediawiki.api.ApiResult;
|
||||||
import org.mediawiki.api.MWApi;
|
import org.mediawiki.api.MWApi;
|
||||||
|
import org.wikimedia.commons.auth.AuthenticatedActivity;
|
||||||
import org.wikimedia.commons.auth.LoginActivity;
|
import org.wikimedia.commons.auth.LoginActivity;
|
||||||
|
import org.wikimedia.commons.auth.WikiAccountAuthenticator;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
|
@ -20,7 +22,11 @@ import android.view.*;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
import android.support.v4.app.NavUtils;
|
import android.support.v4.app.NavUtils;
|
||||||
|
|
||||||
public class ShareActivity extends Activity {
|
public class ShareActivity extends AuthenticatedActivity {
|
||||||
|
|
||||||
|
public ShareActivity() {
|
||||||
|
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
private CommonsApplication app;
|
private CommonsApplication app;
|
||||||
|
|
||||||
|
|
@ -32,21 +38,9 @@ public class ShareActivity extends Activity {
|
||||||
private Uri imageUri;
|
private Uri imageUri;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
protected void onAuthCookieAcquired(String authCookie) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onAuthCookieAcquired(authCookie);
|
||||||
//Actionbar overlay on top of imageview (should be called before .setcontentview)
|
app.getApi().setAuthCookie(authCookie);
|
||||||
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
|
|
||||||
getActionBar().setDisplayShowTitleEnabled(false);
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_share);
|
|
||||||
|
|
||||||
app = (CommonsApplication)this.getApplicationContext();
|
|
||||||
|
|
||||||
backgroundImageView = (ImageView)findViewById(R.id.backgroundImage);
|
|
||||||
titleEdit = (EditText)findViewById(R.id.titleEdit);
|
|
||||||
descEdit = (EditText)findViewById(R.id.descEdit);
|
|
||||||
uploadButton = (Button)findViewById(R.id.uploadButton);
|
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
|
|
||||||
final Context that = this;
|
final Context that = this;
|
||||||
|
|
@ -75,13 +69,27 @@ public class ShareActivity extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
//Actionbar overlay on top of imageview (should be called before .setcontentview)
|
||||||
|
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
|
||||||
|
getActionBar().setDisplayShowTitleEnabled(false);
|
||||||
|
|
||||||
|
setContentView(R.layout.activity_share);
|
||||||
|
|
||||||
|
app = (CommonsApplication)this.getApplicationContext();
|
||||||
|
|
||||||
|
backgroundImageView = (ImageView)findViewById(R.id.backgroundImage);
|
||||||
|
titleEdit = (EditText)findViewById(R.id.titleEdit);
|
||||||
|
descEdit = (EditText)findViewById(R.id.descEdit);
|
||||||
|
uploadButton = (Button)findViewById(R.id.uploadButton);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if(!app.getApi().isLoggedIn) {
|
|
||||||
Intent loginIntent = new Intent(this, LoginActivity.class);
|
|
||||||
this.startActivity(loginIntent);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ public class UploadService extends IntentService {
|
||||||
String notificationTag;
|
String notificationTag;
|
||||||
private int lastPercent = 0;
|
private int lastPercent = 0;
|
||||||
public NotificationUpdateProgressListener(Notification curNotification, String notificationTag) {
|
public NotificationUpdateProgressListener(Notification curNotification, String notificationTag) {
|
||||||
Log.d("Commons", "Fuckity");
|
|
||||||
this.curNotification = curNotification;
|
this.curNotification = curNotification;
|
||||||
this.notificationTag = notificationTag;
|
this.notificationTag = notificationTag;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
143
src/org/wikimedia/commons/auth/AuthenticatedActivity.java
Normal file
143
src/org/wikimedia/commons/auth/AuthenticatedActivity.java
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
package org.wikimedia.commons.auth;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import android.accounts.*;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class AuthenticatedActivity extends Activity {
|
||||||
|
|
||||||
|
|
||||||
|
String accountType;
|
||||||
|
public AuthenticatedActivity(String accountType) {
|
||||||
|
this.accountType = accountType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class GetAuthCookieTask extends AsyncTask<String, String, String> {
|
||||||
|
|
||||||
|
private Account account;
|
||||||
|
private AccountManager accountManager;
|
||||||
|
public GetAuthCookieTask(Account account, AccountManager accountManager) {
|
||||||
|
this.account = account;
|
||||||
|
this.accountManager = accountManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String result) {
|
||||||
|
super.onPostExecute(result);
|
||||||
|
if(result != null) {
|
||||||
|
onAuthCookieAcquired(result);
|
||||||
|
} else {
|
||||||
|
onAuthFailure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... params) {
|
||||||
|
try {
|
||||||
|
return accountManager.blockingGetAuthToken(account, "", false);
|
||||||
|
} catch (OperationCanceledException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (AuthenticatorException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// private class onTokenAcquired implements AccountManagerCallback<Bundle> {
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void run(AccountManagerFuture<Bundle> result) {
|
||||||
|
// Bundle bundle;
|
||||||
|
// try {
|
||||||
|
// bundle = result.getResult();
|
||||||
|
// } catch (OperationCanceledException e) {
|
||||||
|
// // TODO Auto-generated catch block
|
||||||
|
// e.printStackTrace();
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// } catch (AuthenticatorException e) {
|
||||||
|
// // TODO Auto-generated catch block
|
||||||
|
// e.printStackTrace();
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// // TODO Auto-generated catch block
|
||||||
|
// e.printStackTrace();
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
// Log.d("Commons", "Token Found!");
|
||||||
|
// if(bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
||||||
|
// String authCookie = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
||||||
|
// onAuthCookieAcquired(authCookie);
|
||||||
|
// } else {
|
||||||
|
// if(bundle.containsKey(AccountManager.KEY_INTENT)) {
|
||||||
|
// Intent launchIntent = (Intent) bundle.get(AccountManager.KEY_INTENT);
|
||||||
|
// startActivityForResult(launchIntent, 0);
|
||||||
|
// } else {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
|
// super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
// Log.d("Commons", "Result of the loginactivity!");
|
||||||
|
// if(resultCode == Activity.RESULT_OK) {
|
||||||
|
// requestAuthToken();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private void requestAuthToken() {
|
||||||
|
// AccountManager accountManager = AccountManager.get(this);
|
||||||
|
// Account[] allAccounts =accountManager.getAccountsByType(accountType);
|
||||||
|
// if(allAccounts.length == 0) {
|
||||||
|
// Log.d("Commons", "No accounts yet!");
|
||||||
|
// // No Commons Accounts yet!
|
||||||
|
// accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, new onTokenAcquired(), null);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Log.d("Commons", "Accounts found!");
|
||||||
|
// // For now - always pick the first account
|
||||||
|
// Account curAccount = allAccounts[0];
|
||||||
|
// Bundle cookieOptions = new Bundle();
|
||||||
|
// accountManager.getAuthToken(curAccount, "", cookieOptions, this, new onTokenAcquired(), null);
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
AccountManager accountManager = AccountManager.get(this);
|
||||||
|
Account[] allAccounts =accountManager.getAccountsByType(accountType);
|
||||||
|
if(allAccounts.length == 0) {
|
||||||
|
// No Commons Accounts yet!
|
||||||
|
accountManager.addAccount(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE, "", null, null, this, null, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For now - always pick the first account
|
||||||
|
Account curAccount = allAccounts[0];
|
||||||
|
GetAuthCookieTask task = new GetAuthCookieTask(curAccount, accountManager);
|
||||||
|
task.execute("");
|
||||||
|
}
|
||||||
|
protected void onAuthCookieAcquired(String authCookie) {
|
||||||
|
|
||||||
|
}
|
||||||
|
protected void onAuthFailure() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,7 @@ import android.accounts.NetworkErrorException;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
|
|
||||||
|
|
@ -47,7 +48,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getAuthCookie(String username, String password) throws IOException {
|
private String getAuthCookie(String username, String password) throws IOException {
|
||||||
MWApi api = ((CommonsApplication)context.getApplicationContext()).createMWApi();
|
MWApi api = CommonsApplication.createMWApi();
|
||||||
String result = api.login(username, password);
|
String result = api.login(username, password);
|
||||||
if(result.equals("Success")) {
|
if(result.equals("Success")) {
|
||||||
return api.getAuthCookie();
|
return api.getAuthCookie();
|
||||||
|
|
@ -57,14 +58,6 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
|
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
|
||||||
// If the caller requested an authToken type we don't support, then
|
|
||||||
// return an error
|
|
||||||
if (!authTokenType.equals(COMMONS_ACCOUNT_TYPE)) {
|
|
||||||
final Bundle result = new Bundle();
|
|
||||||
result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the username and password from the Account Manager, and ask
|
// Extract the username and password from the Account Manager, and ask
|
||||||
// the server for an appropriate AuthToken.
|
// the server for an appropriate AuthToken.
|
||||||
final AccountManager am = AccountManager.get(context);
|
final AccountManager am = AccountManager.get(context);
|
||||||
|
|
@ -75,6 +68,7 @@ public class WikiAccountAuthenticator extends AbstractAccountAuthenticator {
|
||||||
authCookie = getAuthCookie(account.name, password);
|
authCookie = getAuthCookie(account.name, password);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Network error!
|
// Network error!
|
||||||
|
e.printStackTrace();
|
||||||
throw new NetworkErrorException(e);
|
throw new NetworkErrorException(e);
|
||||||
}
|
}
|
||||||
if (authCookie != null) {
|
if (authCookie != null) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue