Code example for AccountManager

Methods: getAccountsByType, getAuthToken, invalidateAuthToken

0
 
	private final class OAuthGoogleListener implements OnClickListener {
		@Override 
		public void onClick(View v) {	
			authenticator = AUTH_GOOGLE;
			accounts = accountManager.getAccountsByType(ACCOUNT_TYPES[authenticator]);
//			settings = this.getSharedPreferences("prefs", 0); 
			if(accounts.length > 1) {
				showDialog(DIALOG_CHOOSE_ACCOUNT);
			} else { 
				getAuthToken(accounts[0]);
			} 
		} 
	} 
 
	private final class OAuthFacebookListener implements OnClickListener {
		@Override 
		public void onClick(View v) {
			authenticator = AUTH_FACEBOOK;
			accounts = accountManager.getAccountsByType(ACCOUNT_TYPES[authenticator]);
//			settings = this.getSharedPreferences("prefs", 0); 
			if(accounts.length > 1) {
				showDialog(DIALOG_CHOOSE_ACCOUNT);
			} else { 
				getAuthToken(accounts[0]);
			} 
		} 
	} 
 
	private final class FindGoogleContactsListener implements OnClickListener {
 
		@Override 
		public void onClick(View v) {
			final SharedPreferences settings = v.getContext().getSharedPreferences(PREF_NAME, 0);
			String accessToken = settings.getString(PREF_TOKENS[authenticator], "");
			TextView textView = (TextView) findViewById(R.id.google_contacts);
 
			try { 
				Uri.Builder builder = new Uri.Builder().scheme("http").authority("musulogin.appspot.com")
						.path("oauth2users").appendQueryParameter("token", accessToken);
 
				Log.i(TAG, builder.toString());
 
				HttpClient client = new DefaultHttpClient();
				HttpGet request = new HttpGet(builder.toString());
				HttpResponse response = client.execute(request);
				// Get the response 
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						response.getEntity().getContent()));
				String line = "";
				String jsonStr = "";
				while ((line = rd.readLine()) != null) {
					//textView.append(line); 
					jsonStr += line;
				} 
 
				JSONObject object = new JSONObject(jsonStr);
				String keysStr = object.getString("keys");
				JSONArray keys = new JSONArray(keysStr);
				textView.setText("");
 
				friendList = new ArrayList<Pair<String, String>>(keys.length());
				for(int i = 0; i < keys.length(); i++){
					JSONObject friend = keys.getJSONObject(i);
					textView.append(friend.getString("key") + "->" + friend.getString("uid") + "\n");
					friendList.add(new Pair<String, String>(friend.getString("uid"), friend.getString("key")));
				} 
 
				showDialog(DIALOG_CHOOSE_FRIEND_TO_ADD);
			} catch (Exception e) {
				Log.e(TAG, e.toString());
			}  
		} 
 
	} 
 
	@Override 
	protected Dialog onCreateDialog(int id){
		Dialog dialog = null;
		switch(id) {
		case DIALOG_CHOOSE_ACCOUNT:
			final CharSequence[] items = new CharSequence[accounts.length];
			for(int i = 0; i < accounts.length; i++)
				items[i] = accounts[i].name;
			AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
			builder1.setTitle("Choose an account");
			builder1.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int item) {
			        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
			        getAuthToken(accounts[item]);
			        dialog.dismiss();
			    } 
			}); 
			AlertDialog chooseAccountDlg = builder1.create();
			dialog = chooseAccountDlg;
			break; 
		case DIALOG_CHOOSE_FRIEND_TO_ADD:
			final CharSequence[] friendIds = new CharSequence[friendList.size()];
			int i = 0;
			for(Pair<String, String> f : friendList){
				friendIds[i++] = f.first;
			} 
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setTitle("Choose a friend");
			builder2.setSingleChoiceItems(friendIds, 0, new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int item) {
			        Toast.makeText(getApplicationContext(), friendIds[item], Toast.LENGTH_SHORT).show();
			        //getAuthToken(accounts[item]); 
			        addContactAsFriend(friendList.get(item).first, friendList.get(item).second);
			        dialog.dismiss();
			    } 
			}); 
			AlertDialog chooseFriendDlg = builder2.create();
			dialog = chooseFriendDlg;
			break; 
		default: 
			dialog = null;
		} 
 
		return dialog;
	} 
 
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.oauth_google);
 
		findViewById(R.id.authenticate_google).setOnClickListener(
				new OAuthGoogleListener()); 
		findViewById(R.id.authenticate_facebook).setOnClickListener(
				new OAuthFacebookListener()); 
 
		findViewById(R.id.find_google_contacts).setOnClickListener(
				new FindGoogleContactsListener()); 
 
		accountManager = AccountManager.get(this);
	} 
 
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUEST_AUTHENTICATE:
			if (resultCode == RESULT_OK) {
				getAuthToken(accounts[0]);
			} else { 
				// user denied 
			} 
			break;  
		} 
	} 
 
	private void addContactAsFriend(String email, String pubkey){
		RSACrypto.publicKeyFromString(pubkey);
		Uri uri = Helpers.insertContact(this, pubkey, email, email);
		long contactId = Long.valueOf(uri.getLastPathSegment());
		Helpers.insertSubscriber(this, contactId, "friend");
 
		//FriendRequest.sendFriendRequest(this,contactId, null); 
		String emailList[] = {email};
		Intent share = new Intent(Intent.ACTION_SEND);
		Uri friendRequest = FriendRequest.getInvitationUri(this);
		share.putExtra(Intent.EXTRA_TEXT,
				"Be my friend on Musubi! Click here from your Android device: " 
						+ friendRequest);
		share.putExtra(Intent.EXTRA_SUBJECT, "Join me on Musubi!");
		share.putExtra(Intent.EXTRA_EMAIL, emailList);
		share.setType("text/plain");
		startActivity(share);
	} 
 
	private void getAuthToken(final Account account) {
 
		final SharedPreferences settings = this.getSharedPreferences(PREF_NAME, 0);
		String accessToken = settings.getString(PREF_TOKENS[authenticator], "");
 
		accountManager.invalidateAuthToken(ACCOUNT_TYPES[authenticator], accessToken);
		accountManager.getAuthToken(account, AUTH_SCOPES[authenticator], true, new AccountManagerCallback<Bundle>() {
			public void run(AccountManagerFuture<Bundle> future) {
				try { 
					Bundle bundle = future.getResult();
					if (bundle.containsKey(AccountManager.KEY_INTENT)) {
						Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);