Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends ActionBarActivity {
- final static private String APP_KEY = "key_here";
- final static private String APP_SECRET = "secret_here";
- final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
- private static final boolean USE_OAUTH1 = false;
- // You don't need to change these, leave them alone.
- final static private String ACCOUNT_PREFS_NAME = "prefs";
- final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
- final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
- // In the class declaration section;
- protected DropboxAPI<AndroidAuthSession> mDBApi;
- static String[] fnames;
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- // Notice that setContentView() is not used, because we use the root
- // android.R.id.content as the container for each fragment
- // setup action bar for tabs
- ActionBar actionBar = getSupportActionBar();
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- actionBar.setDisplayShowTitleEnabled(true);
- final String info = "Info";
- final String db = "Dropbox";
- Tab tab = actionBar.newTab()
- .setText(info)
- .setTabListener(new TabListener<InfoFragment>(
- this,"info", InfoFragment.class));
- actionBar.addTab(tab);
- tab = actionBar.newTab()
- .setText(db)
- .setTabListener(new TabListener<DbFragment>(
- this, "dropbox", DbFragment.class));
- actionBar.addTab(tab);
- // And later in some initialization function:
- AndroidAuthSession session = buildSession();
- mDBApi = new DropboxAPI<AndroidAuthSession>(session);
- if (USE_OAUTH1){
- mDBApi.getSession().startAuthentication(MainActivity.this);
- } else{
- mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
- }
- int i = 0;
- fnames = null;
- Entry entries;
- ArrayList<Entry> files = new ArrayList<Entry>();
- ArrayList<String> dir = new ArrayList<String>();
- try {
- entries = mDBApi.metadata("/", 100, null, true, null);
- for (Entry e: entries.contents){
- if (!e.isDeleted){
- files.add(e);
- dir.add(new String(files.get(i++).path));
- }
- }
- } catch (DropboxException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- fnames = dir.toArray(new String[dir.size()]);
- }
- public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
- private Fragment mFragment;
- private final Activity mActivity;
- private final String mTag;
- private final Class<T> mClass;
- /** Constructor used each time a new tab is created.
- * @param activity The host Activity, used to instantiate the fragment
- * @param tag The identifier tag for the fragment
- * @param clz The fragment's Class, used to instantiate the fragment
- */
- public TabListener(Activity activity, String tag, Class<T> clz) {
- mActivity = activity;
- mTag = tag;
- mClass = clz;
- }
- /* The following are each of the ActionBar.TabListener callbacks */
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- // Check if the fragment is already initialized
- if (mFragment == null) {
- // If not, instantiate and add it to the activity
- mFragment = Fragment.instantiate(mActivity, mClass.getName());
- ft.add(android.R.id.content, mFragment, mTag);
- } else {
- // If it exists, simply attach it in order to show it
- ft.attach(mFragment);
- }
- }
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- if (mFragment != null) {
- // Detach the fragment, because another one is being attached
- ft.detach(mFragment);
- }
- }
- public void onTabReselected(Tab tab, FragmentTransaction ft) {
- // User selected the already selected tab. Usually do nothing.
- }
- }
- @Override
- public void onResume(){
- super.onResume();
- if (mDBApi.getSession().authenticationSuccessful()){
- try{
- //Required to complete auth, sets the access token on the session
- mDBApi.getSession().finishAuthentication();
- String accessToken = mDBApi.getSession().getOAuth2AccessToken();
- } catch (IllegalStateException e ){
- Log.i("DBAuthLog", "Error authenticating",e );
- }
- }
- }
- private void loadAuth(AndroidAuthSession session) {
- SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
- String key = prefs.getString(ACCESS_KEY_NAME, null);
- String secret = prefs.getString(ACCESS_SECRET_NAME, null);
- if (key == null || secret == null || key.length() == 0 || secret.length() == 0) return;
- if (key.equals("oauth2:")) {
- // If the key is set to "oauth2:", then we can assume the token is for OAuth 2.
- session.setOAuth2AccessToken(secret);
- } else {
- // Still support using old OAuth 1 tokens.
- session.setAccessTokenPair(new AccessTokenPair(key, secret));
- }
- }
- /**
- * Shows keeping the access keys returned from Trusted Authenticator in a local
- * store, rather than storing user name & password, and re-authenticating each
- * time (which is not to be done, ever).
- */
- private void storeAuth(AndroidAuthSession session) {
- // Store the OAuth 2 access token, if there is one.
- String oauth2AccessToken = session.getOAuth2AccessToken();
- if (oauth2AccessToken != null) {
- SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
- Editor edit = prefs.edit();
- edit.putString(ACCESS_KEY_NAME, "oauth2:");
- edit.putString(ACCESS_SECRET_NAME, oauth2AccessToken);
- edit.commit();
- return;
- }
- // Store the OAuth 1 access token, if there is one. This is only necessary if
- // you're still using OAuth 1.
- AccessTokenPair oauth1AccessToken = session.getAccessTokenPair();
- if (oauth1AccessToken != null) {
- SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
- Editor edit = prefs.edit();
- edit.putString(ACCESS_KEY_NAME, oauth1AccessToken.key);
- edit.putString(ACCESS_SECRET_NAME, oauth1AccessToken.secret);
- edit.commit();
- return;
- }
- }
- private AndroidAuthSession buildSession() {
- AppKeyPair appKeyPair = new AppKeyPair(MainActivity.APP_KEY, MainActivity.APP_SECRET);
- AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
- loadAuth(session);
- return session;
- }
- }
- public class DbFragment extends ListFragment {
- private ListView mListView;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- String[] xfnames = MainActivity.fnames;
- ListView listView = new ListView(getActivity());
- ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
- android.R.layout.simple_list_item_1, xfnames);
- for (String str: xfnames)
- array.add(str);
- setListAdapter(array);
- return super.onCreateView(inflater, container, savedInstanceState);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello">
- android:layout_marginTop="40dp"
- android:textSize="40sp"/>
- </TextView>
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </ListView>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </ListView>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement