Back to project page AndroidWifiServer.
The source code is released under:
Apache License
If you think the Android project AndroidWifiServer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jp.maju.wifiserver.twitter; //w w w . ja v a 2s . c o m import jp.maju.wifiserver.AsyncExecutionTask; import jp.maju.wifiserver.R; import jp.maju.wifiserver.camera.QRReaderActivity; import jp.maju.wifiserver.server.ServerActivity; import jp.maju.wifiserver.twitter.ProxyDialogFragment.OnSetProxyListener; import jp.maju.wifiserver.util.Logger; import jp.maju.wifiserver.util.PreferenceUtil; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Toast; public class TwitterOAuthActivity extends ActionBarActivity implements OnSetProxyListener { private static final String TAG = TwitterOAuthActivity.class .getSimpleName(); public static final String KEY_IS_SERVER = "keyIsServer"; private String mCallbackURL; private Twitter mTwitter; private RequestToken mRequestToken; private boolean isServer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent() == null) { finish(); return; } isServer = getIntent().getBooleanExtra(KEY_IS_SERVER, false); if (TwitterUtils.hasAccessToken(getApplicationContext(), isServer)) { transition2(); return; } setContentView(R.layout.activity_twitter_oauth); mCallbackURL = getString(R.string.twitter_callback_url); // getString(R.string.twitter_callback_scheme), // getString(R.string.twitter_callback_domain)); findViewById(R.id.action_start_oauth).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // ProxyDialogFragment.show(getSupportFragmentManager()); mTwitter = TwitterUtils.getRegisteredTwitterInstance( getApplication(), isServer); startAuthorize(); } }); } private void startAuthorize() { AsyncExecutionTask<Void, Void, String> task = new AsyncExecutionTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { try { mRequestToken = mTwitter.getOAuthRequestToken(mCallbackURL); return mRequestToken.getAuthorizationURL(); } catch (TwitterException e) { Logger.e(TAG, e); } return null; } @Override protected void onPostExecute(String url) { if (url != null) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); Logger.d(TAG, "Go for auth:" + url); } else { Logger.d(TAG, "RETURN CALLBACK Faild"); } } }; task.exec(); } @Override public void onNewIntent(Intent intent) { if (intent == null) { Logger.d(TAG, "Intent is null"); return; } if (intent.getData() == null) { Logger.d(TAG, "Intent data is null"); return; } if (!intent.getData().toString().startsWith(mCallbackURL)) { Logger.d(TAG, "Date is unmatched." + intent.getData().toString() + "/and/" + mCallbackURL); return; } String verifier = intent.getData().getQueryParameter( getString(R.string.authorization_query)); AsyncExecutionTask<String, Void, AccessToken> task = new AsyncExecutionTask<String, Void, AccessToken>() { @Override protected AccessToken doInBackground(String... params) { if (params == null || mRequestToken == null) { return null; } try { return mTwitter.getOAuthAccessToken(mRequestToken, params[0]); } catch (TwitterException e) { Logger.e(TAG, e); } return null; } @Override protected void onPostExecute(AccessToken accessToken) { if (accessToken != null) { // case: success successOAuth(accessToken); } else { // case: faild or cancel Toast.makeText(TwitterOAuthActivity.this, R.string.error_authorization, Toast.LENGTH_SHORT) .show(); } } }; task.exec(verifier); } private void successOAuth(AccessToken accessToken) { TwitterUtils.storeAccessToken(this, accessToken, isServer); transition2(); } private void transition2() { if (isServer) { Intent intent = new Intent(this, ServerActivity.class); PreferenceUtil.setDefaultSide(getApplication(), 1); startActivity(intent); } else { Intent intent = new Intent(this, QRReaderActivity.class); startActivity(intent); } finish(); } @Override public void onComplete() { mTwitter = TwitterUtils.getRegisteredTwitterInstance(getApplication(), isServer); startAuthorize(); } }