Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. public class MainActivity extends ActionBarActivity {
  2.  
  3. final static private String APP_KEY = "key_here";
  4. final static private String APP_SECRET = "secret_here";
  5. final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
  6. private static final boolean USE_OAUTH1 = false;
  7.  
  8. // You don't need to change these, leave them alone.
  9. final static private String ACCOUNT_PREFS_NAME = "prefs";
  10. final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
  11. final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
  12.  
  13.  
  14. // In the class declaration section;
  15. protected DropboxAPI<AndroidAuthSession> mDBApi;
  16. static String[] fnames;
  17.  
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState){
  21. super.onCreate(savedInstanceState);
  22. // Notice that setContentView() is not used, because we use the root
  23. // android.R.id.content as the container for each fragment
  24.  
  25.  
  26. // setup action bar for tabs
  27. ActionBar actionBar = getSupportActionBar();
  28. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  29. actionBar.setDisplayShowTitleEnabled(true);
  30.  
  31. final String info = "Info";
  32. final String db = "Dropbox";
  33. Tab tab = actionBar.newTab()
  34. .setText(info)
  35. .setTabListener(new TabListener<InfoFragment>(
  36. this,"info", InfoFragment.class));
  37. actionBar.addTab(tab);
  38.  
  39. tab = actionBar.newTab()
  40. .setText(db)
  41. .setTabListener(new TabListener<DbFragment>(
  42. this, "dropbox", DbFragment.class));
  43. actionBar.addTab(tab);
  44.  
  45. // And later in some initialization function:
  46. AndroidAuthSession session = buildSession();
  47. mDBApi = new DropboxAPI<AndroidAuthSession>(session);
  48.  
  49.  
  50. if (USE_OAUTH1){
  51. mDBApi.getSession().startAuthentication(MainActivity.this);
  52. } else{
  53. mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
  54. }
  55.  
  56. int i = 0;
  57. fnames = null;
  58. Entry entries;
  59. ArrayList<Entry> files = new ArrayList<Entry>();
  60. ArrayList<String> dir = new ArrayList<String>();
  61. try {
  62. entries = mDBApi.metadata("/", 100, null, true, null);
  63. for (Entry e: entries.contents){
  64. if (!e.isDeleted){
  65. files.add(e);
  66. dir.add(new String(files.get(i++).path));
  67. }
  68. }
  69. } catch (DropboxException e1) {
  70. // TODO Auto-generated catch block
  71. e1.printStackTrace();
  72. }
  73. fnames = dir.toArray(new String[dir.size()]);
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
  81. private Fragment mFragment;
  82. private final Activity mActivity;
  83. private final String mTag;
  84. private final Class<T> mClass;
  85.  
  86. /** Constructor used each time a new tab is created.
  87. * @param activity The host Activity, used to instantiate the fragment
  88. * @param tag The identifier tag for the fragment
  89. * @param clz The fragment's Class, used to instantiate the fragment
  90. */
  91. public TabListener(Activity activity, String tag, Class<T> clz) {
  92. mActivity = activity;
  93. mTag = tag;
  94. mClass = clz;
  95. }
  96.  
  97. /* The following are each of the ActionBar.TabListener callbacks */
  98.  
  99. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  100. // Check if the fragment is already initialized
  101. if (mFragment == null) {
  102. // If not, instantiate and add it to the activity
  103. mFragment = Fragment.instantiate(mActivity, mClass.getName());
  104. ft.add(android.R.id.content, mFragment, mTag);
  105. } else {
  106. // If it exists, simply attach it in order to show it
  107. ft.attach(mFragment);
  108. }
  109. }
  110.  
  111. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  112. if (mFragment != null) {
  113. // Detach the fragment, because another one is being attached
  114. ft.detach(mFragment);
  115. }
  116. }
  117.  
  118. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  119. // User selected the already selected tab. Usually do nothing.
  120. }
  121. }
  122.  
  123. @Override
  124. public void onResume(){
  125. super.onResume();
  126. if (mDBApi.getSession().authenticationSuccessful()){
  127. try{
  128. //Required to complete auth, sets the access token on the session
  129. mDBApi.getSession().finishAuthentication();
  130.  
  131. String accessToken = mDBApi.getSession().getOAuth2AccessToken();
  132. } catch (IllegalStateException e ){
  133. Log.i("DBAuthLog", "Error authenticating",e );
  134. }
  135. }
  136. }
  137.  
  138. private void loadAuth(AndroidAuthSession session) {
  139. SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
  140. String key = prefs.getString(ACCESS_KEY_NAME, null);
  141. String secret = prefs.getString(ACCESS_SECRET_NAME, null);
  142. if (key == null || secret == null || key.length() == 0 || secret.length() == 0) return;
  143.  
  144. if (key.equals("oauth2:")) {
  145. // If the key is set to "oauth2:", then we can assume the token is for OAuth 2.
  146. session.setOAuth2AccessToken(secret);
  147. } else {
  148. // Still support using old OAuth 1 tokens.
  149. session.setAccessTokenPair(new AccessTokenPair(key, secret));
  150. }
  151. }
  152.  
  153.  
  154. /**
  155. * Shows keeping the access keys returned from Trusted Authenticator in a local
  156. * store, rather than storing user name & password, and re-authenticating each
  157. * time (which is not to be done, ever).
  158. */
  159. private void storeAuth(AndroidAuthSession session) {
  160. // Store the OAuth 2 access token, if there is one.
  161. String oauth2AccessToken = session.getOAuth2AccessToken();
  162. if (oauth2AccessToken != null) {
  163. SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
  164. Editor edit = prefs.edit();
  165. edit.putString(ACCESS_KEY_NAME, "oauth2:");
  166. edit.putString(ACCESS_SECRET_NAME, oauth2AccessToken);
  167. edit.commit();
  168. return;
  169. }
  170. // Store the OAuth 1 access token, if there is one. This is only necessary if
  171. // you're still using OAuth 1.
  172. AccessTokenPair oauth1AccessToken = session.getAccessTokenPair();
  173. if (oauth1AccessToken != null) {
  174. SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
  175. Editor edit = prefs.edit();
  176. edit.putString(ACCESS_KEY_NAME, oauth1AccessToken.key);
  177. edit.putString(ACCESS_SECRET_NAME, oauth1AccessToken.secret);
  178. edit.commit();
  179. return;
  180. }
  181. }
  182.  
  183. private AndroidAuthSession buildSession() {
  184. AppKeyPair appKeyPair = new AppKeyPair(MainActivity.APP_KEY, MainActivity.APP_SECRET);
  185.  
  186. AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
  187. loadAuth(session);
  188. return session;
  189. }
  190.  
  191. }
  192.  
  193. public class DbFragment extends ListFragment {
  194.  
  195. private ListView mListView;
  196. @Override
  197. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  198. Bundle savedInstanceState) {
  199.  
  200.  
  201. String[] xfnames = MainActivity.fnames;
  202.  
  203.  
  204. ListView listView = new ListView(getActivity());
  205. ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
  206. android.R.layout.simple_list_item_1, xfnames);
  207.  
  208. for (String str: xfnames)
  209. array.add(str);
  210. setListAdapter(array);
  211.  
  212.  
  213.  
  214. return super.onCreateView(inflater, container, savedInstanceState);
  215. }
  216.  
  217.  
  218.  
  219.  
  220. }
  221.  
  222. <?xml version="1.0" encoding="utf-8"?>
  223. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  224. android:layout_width="match_parent"
  225. android:layout_height="match_parent"
  226. android:orientation="vertical" >
  227.  
  228. <TextView
  229. android:layout_width="wrap_content"
  230. android:layout_height="wrap_content"
  231. android:text="@string/hello">
  232. android:layout_marginTop="40dp"
  233. android:textSize="40sp"/>
  234. </TextView>
  235.  
  236. <ListView
  237. android:id="@android:id/list"
  238. android:layout_width="fill_parent"
  239. android:layout_height="fill_parent">
  240.  
  241. </ListView>
  242.  
  243.  
  244. </LinearLayout>
  245.  
  246. <?xml version="1.0" encoding="utf-8"?>
  247. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  248. android:layout_width="match_parent"
  249. android:layout_height="match_parent"
  250. android:orientation="vertical" >
  251. <ListView
  252. android:id="@android:id/list"
  253. android:layout_width="fill_parent"
  254. android:layout_height="fill_parent">
  255.  
  256. </ListView>
  257.  
  258.  
  259. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement