package com.wuman.oauth.samples.linkedin; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.widget.Button; import android.widget.TextView; import com.google.api.client.auth.oauth2.BearerToken; import com.google.api.client.auth.oauth2.ClientParametersAuthentication; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.http.GenericUrl; import com.wuman.android.auth.AuthorizationDialogController; import com.wuman.android.auth.AuthorizationFlow; import com.wuman.android.auth.DialogFragmentController; import com.wuman.android.auth.OAuthManager; import com.wuman.android.auth.oauth2.store.SharedPreferencesCredentialStore; import com.wuman.oauth.samples.AsyncResourceLoader; import com.wuman.oauth.samples.AsyncResourceLoader.Result; import com.wuman.oauth.samples.OAuth; import com.wuman.oauth.samples.R; import com.wuman.oauth.samples.SamplesActivity; import com.wuman.oauth.samples.SamplesConstants; import java.io.IOException; import java.util.Arrays; import java.util.logging.Logger; import de.keyboardsurfer.android.widget.crouton.Crouton; import de.keyboardsurfer.android.widget.crouton.Style; public class SimpleOAuth2ExplicitActivity extends FragmentActivity { static final Logger LOGGER = Logger.getLogger(SamplesConstants.TAG); @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); super.onCreate(savedInstanceState); FragmentManager fm = getSupportFragmentManager(); if (fm.findFragmentById(android.R.id.content) == null) { OAuthFragment list = new OAuthFragment(); fm.beginTransaction().add(android.R.id.content, list).commit(); } } @Override protected void onDestroy() { Crouton.cancelAllCroutons(); super.onDestroy(); } public static class OAuthFragment extends Fragment implements LoaderManager.LoaderCallbacks<Result<Credential>> { private static final int LOADER_GET_TOKEN = 0; private static final int LOADER_DELETE_TOKEN = 1; private OAuthManager oauth; private Button button; private TextView message; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.delete_cookies_menu, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.delete_cookies: { CookieSyncManager.createInstance(getActivity()); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); return true; } default: { return super.onOptionsItemSelected(item); } } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.oauth_login, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { button = (Button) view.findViewById(android.R.id.button1); setButtonText(R.string.get_token); message = (TextView) view.findViewById(android.R.id.text1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (v.getTag().equals(R.string.get_token)) { if (getLoaderManager().getLoader(LOADER_GET_TOKEN) == null) { getLoaderManager().initLoader(LOADER_GET_TOKEN, null, OAuthFragment.this); } else { getLoaderManager().restartLoader(LOADER_GET_TOKEN, null, OAuthFragment.this); } } else { // R.string.delete_token if (getLoaderManager().getLoader(LOADER_DELETE_TOKEN) == null) { getLoaderManager().initLoader(LOADER_DELETE_TOKEN, null, OAuthFragment.this); } else { getLoaderManager().restartLoader(LOADER_DELETE_TOKEN, null, OAuthFragment.this); } } } }); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); boolean fullScreen = getActivity().getSharedPreferences("Preference", 0) .getBoolean(SamplesActivity.KEY_AUTH_MODE, false); // setup credential store SharedPreferencesCredentialStore credentialStore = new SharedPreferencesCredentialStore(getActivity(), SamplesConstants.CREDENTIALS_STORE_PREF_FILE, OAuth.JSON_FACTORY); // setup authorization flow AuthorizationFlow flow = new AuthorizationFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), OAuth.HTTP_TRANSPORT, OAuth.JSON_FACTORY, new GenericUrl(LinkedInConstants.OAUTH2_TOKEN_SERVER_URL), new ClientParametersAuthentication(LinkedInConstants.CLIENT_ID, LinkedInConstants.CLIENT_SECRET), LinkedInConstants.CLIENT_ID, LinkedInConstants.AUTHORIZATION_CODE_SERVER_URL) .setScopes(Arrays.asList(LinkedInScopes.READ_BASICPROFILE, LinkedInScopes.READ_FULLPROFILE)) .setCredentialStore(credentialStore) .build(); // setup UI controller AuthorizationDialogController controller = new DialogFragmentController(getFragmentManager(), fullScreen) { @Override public String getRedirectUri() throws IOException { return LinkedInConstants.REDIRECT_URL; } @Override public boolean isJavascriptEnabledForWebView() { return true; } @Override public boolean disableWebViewCache() { return false; } @Override public boolean removePreviousCookie() { return false; } }; // instantiate an OAuthManager instance oauth = new OAuthManager(flow, controller); } @Override public Loader<Result<Credential>> onCreateLoader(int id, Bundle args) { getActivity().setProgressBarIndeterminateVisibility(true); button.setEnabled(false); message.setText(""); if (id == LOADER_GET_TOKEN) { return new GetTokenLoader(getActivity(), oauth); } else { return new DeleteTokenLoader(getActivity(), oauth); } } @Override public void onLoadFinished(Loader<Result<Credential>> loader, Result<Credential> result) { if (loader.getId() == LOADER_GET_TOKEN) { message.setText(result.success ? result.data.getAccessToken() : ""); } else { message.setText(""); } if (result.success) { if (loader.getId() == LOADER_GET_TOKEN) { setButtonText(R.string.delete_token); } else { setButtonText(R.string.get_token); } } else { setButtonText(R.string.get_token); Crouton.makeText(getActivity(), result.errorMessage, Style.ALERT).show(); } getActivity().setProgressBarIndeterminateVisibility(false); button.setEnabled(true); } @Override public void onLoaderReset(Loader<Result<Credential>> loader) { message.setText(""); getActivity().setProgressBarIndeterminateVisibility(false); button.setEnabled(true); } @Override public void onDestroy() { getLoaderManager().destroyLoader(LOADER_GET_TOKEN); getLoaderManager().destroyLoader(LOADER_DELETE_TOKEN); super.onDestroy(); } private void setButtonText(int action) { button.setText(action); button.setTag(action); } private static class GetTokenLoader extends AsyncResourceLoader<Credential> { private final OAuthManager oauth; public GetTokenLoader(Context context, OAuthManager oauth) { super(context); this.oauth = oauth; } @Override public Credential loadResourceInBackground() throws Exception { Credential credential = oauth.authorizeExplicitly(getContext().getString(R.string.token_linkedin_explicit), null, null).getResult(); LOGGER.info("token: " + credential.getAccessToken()); return credential; } @Override public void updateErrorStateIfApplicable(AsyncResourceLoader.Result<Credential> result) { Credential data = result.data; result.success = !TextUtils.isEmpty(data.getAccessToken()); result.errorMessage = result.success ? null : "error"; } } private static class DeleteTokenLoader extends AsyncResourceLoader<Credential> { private final OAuthManager oauth; private boolean success; public DeleteTokenLoader(Context context, OAuthManager oauth) { super(context); this.oauth = oauth; } @Override public Credential loadResourceInBackground() throws Exception { success = oauth.deleteCredential(getContext().getString(R.string.token_linkedin_explicit), null, null).getResult(); LOGGER.info("token deleted: " + success); return null; } @Override public void updateErrorStateIfApplicable(Result<Credential> result) { result.success = success; result.errorMessage = result.success ? null : "error"; } } } }