Code example for Activity

Methods: getDefaultSharedPreferences

0
		return secret;
	} 
 
	private void dismiss(){ 
		if(dialog != null){
			new AQuery(act).dismiss(dialog);
			dialog = null;
		} 
	} 
	 
	private void show(){ 
		if(dialog != null){
			new AQuery(act).show(dialog);
		} 
	} 
	 
	private void failure(){ 
		dismiss(); 
		failure(act, 401, "cancel");
	} 
	 
	protected void auth() { 
 
		Task task = new Task();
		task.execute();
		 
	} 
	 
	private class Task extends AsyncTask<String, String, String> implements OnCancelListener, Runnable{
		 
		private AbstractAjaxCallback<?, ?> cb;
		 
		@Override 
		protected String doInBackground(String... params) {
	       
			String url = null;
			try { 
				url = provider.retrieveRequestToken(consumer, CALLBACK_URI);
			}catch(Exception e){
				AQUtility.report(e);				
				return null; 
			}  
			 
			return url;
		} 
 
		 
		@Override 
		protected void onPostExecute(String url) {
			 
			if(url != null){
				 
				dialog = new WebDialog(act, url, new TwWebViewClient());		
				dialog.setOnCancelListener(this);			
				show(); 
				//dialog.hide(); 
				 
				dialog.load();
				 
			}else{ 
				failure(); 
			} 
			 
		} 
 
 
		@Override 
		public void onCancel(DialogInterface arg0) {
			failure(); 
		} 
 
 
		@Override 
		public void run() { 
			auth(cb);
		} 
	 
		 
	 
	} 
	 
	public void authenticate(boolean refreshToken){
		 
		if(!refreshToken && token != null && secret != null){
			authenticated(secret, token);
		}else{ 
			auth(); 
		} 
		 
	} 
	 
	protected void authenticated(String secret, String token){
		 
	} 
	 
	private static final String TW_TOKEN = "aq.tw.token";
	private static final String TW_SECRET = "aq.tw.secret";
	 
	private String fetchToken(String key){
		return PreferenceManager.getDefaultSharedPreferences(act).getString(key, null);	
	} 
	 
	private void storeToken(String key1, String token1, String key2, String token2){
		PreferenceManager.getDefaultSharedPreferences(act).edit().putString(key1, token1).putString(key2, token2).commit();	
	} 
 
	private String extract(String url, String param){
		 
		Uri uri = Uri.parse(url);
		String value = uri.getQueryParameter(param);
		return value;
		 
	} 
 
	private class Task2 extends AsyncTask<String, String, String>{
 
		@Override 
		protected String doInBackground(String... params) {
			 
			try { 
				provider.retrieveAccessToken(consumer, params[0]);
			}catch(Exception e) {
				AQUtility.report(e);
				return null; 
			}  
			 
			return ""; 
		} 
		 
		@Override 
		protected void onPostExecute(String url) {
			 
			if(url != null){
				 
				token = consumer.getToken();
				secret = consumer.getTokenSecret();
				 
				AQUtility.debug("token", token);
				AQUtility.debug("secret", secret);
				 
				storeToken(TW_TOKEN, token, TW_SECRET, secret);
				 
				dismiss(); 
				success(act);
				 
				authenticated(secret, token);
			}else{ 
				 
				failure();