Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from mobilejazz/provider/dropbox
Browse files Browse the repository at this point in the history
Dropbox Provider
  • Loading branch information
widmoser committed Jan 29, 2015
2 parents cfe9686 + cc342b3 commit ac1000a
Show file tree
Hide file tree
Showing 58 changed files with 922 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
import android.widget.Toast;

import com.mobilejazz.coltrane.library.DocumentUriProvider;
import com.mobilejazz.coltrane.library.DocumentsProvider;
import com.mobilejazz.coltrane.library.DocumentsProviderRegistry;
import com.mobilejazz.coltrane.library.Root;
import com.mobilejazz.coltrane.library.compatibility.DocumentsContract;
import com.mobilejazz.coltrane.library.utils.AsyncLoader;
import com.mobilejazz.coltrane.library.utils.DocumentCursor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -55,9 +57,10 @@
* Main Activity that handles the FileListFragments
*/
public class DocumentBrowserActivity extends Activity implements
FragmentManager.OnBackStackChangedListener, DocumentListFragment.Callbacks, LoaderManager.LoaderCallbacks<List<Root>> {
FragmentManager.OnBackStackChangedListener, DocumentListFragment.Callbacks, LoaderManager.LoaderCallbacks<List<Root>>, ProviderChooser.OnProviderSelectedListener {

private static final int LOADER_ROOTS = 1;
private static final int REQUEST_NEW_ACCOUNT = 35093;

public static final String PATH = "com.mobilejazz.coltrane.ui.browser.path";
public static final String SELECTED_ITEM = "com.mobilejazz.coltrane.ui.browser.selected";
Expand Down Expand Up @@ -110,6 +113,7 @@ public void onNothingSelected(AdapterView<?> parent) {
private ArrayAdapter<Root> mDrawerAdapter;
private BackStackAdapter mNavigationAdapter;
private Map<Root, Integer> mRootIndices;
private ArrayList<String> mLinkableProviderIds;

private Bundle mSavedInstanceState;

Expand All @@ -121,6 +125,16 @@ protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.picker);

mLinkableProviderIds = new ArrayList<String>();
for (DocumentsProvider p : DocumentsProviderRegistry.get().getAll()) {
if (p.linkAccount() != null) {
mLinkableProviderIds.add(p.getId());
}
}
if (mLinkableProviderIds.isEmpty()) {
findViewById(R.id.link_account).setVisibility(View.GONE);
}

mHandler = new Handler();

// Setting up the navigation:
Expand Down Expand Up @@ -227,6 +241,17 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_NEW_ACCOUNT) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, R.string.account_linked, Toast.LENGTH_LONG).show();
getLoaderManager().restartLoader(LOADER_ROOTS, null, this);
}
}
}

private void populateRootIndices() {
mRootIndices = new HashMap<Root, Integer>();
for (int pos = 0; pos < mDrawerAdapter.getCount(); ++pos) {
Expand Down Expand Up @@ -329,6 +354,14 @@ private void unregisterStorageListener() {
unregisterReceiver(mStorageListener);
}

public void linkAccount(View v) {
if (mLinkableProviderIds.size() > 1) {
new ProviderChooser().show(getFragmentManager(), "ProviderChooser");
} else {
DocumentsProviderRegistry.get().getProvider(mLinkableProviderIds.get(0)).linkAccount().performWith(this, REQUEST_NEW_ACCOUNT);
}
}

@Override
public Loader<List<Root>> onCreateLoader(int id, Bundle args) {
return new AsyncLoader<List<Root>>(this) {
Expand Down Expand Up @@ -368,6 +401,12 @@ public void onLoaderReset(Loader<List<Root>> loader) {
mDrawerAdapter.clear();
}

@Override
public void onProviderSelected(DocumentsProvider provider) {
// link new account:
provider.linkAccount().performWith(this, REQUEST_NEW_ACCOUNT);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.mobilejazz.coltrane.library.DocumentsProvider;
import com.mobilejazz.coltrane.library.DocumentsProviderRegistry;
import com.mobilejazz.coltrane.library.action.PendingAction;
import com.mobilejazz.coltrane.library.utils.DocumentCursor;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public interface Callbacks {
private View mListContainer;
private SwipeRefreshLayout mSwipeLayout;

private Intent mPendingAction;
private PendingAction mPendingAction;

/**
* Create a new instance with the given file path.
Expand Down Expand Up @@ -124,7 +125,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public void onClick(View v) {
changeEmptyView(mEmptyTextView);
setListShown(false);
startActivityForResult(mPendingAction, REQUEST_RESOLVE_PROVIDER_ISSUE);
mPendingAction.performWith(DocumentListFragment.this, REQUEST_RESOLVE_PROVIDER_ISSUE);
}
});
mEmptyTextView = (TextView)view.findViewById(R.id.empty);
Expand Down Expand Up @@ -186,12 +187,12 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
setListShownNoAnimation(true);


Intent pendingAction = ((DocumentLoader)loader).getPendingAction();
PendingAction pendingAction = ((DocumentLoader)loader).getPendingAction();
if (pendingAction != null) {
if (mPendingAction == null) {
mPendingAction = pendingAction;
changeEmptyView(mPendingActionView);
startActivityForResult(mPendingAction, REQUEST_RESOLVE_PROVIDER_ISSUE);
mPendingAction.performWith(this, REQUEST_RESOLVE_PROVIDER_ISSUE);
}
} else if (data == null) {
// network error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.mobilejazz.coltrane.library.DocumentsProvider;
import com.mobilejazz.coltrane.library.UserRecoverableException;
import com.mobilejazz.coltrane.library.action.PendingAction;
import com.mobilejazz.coltrane.library.compatibility.DocumentsContract;

import java.io.FileNotFoundException;
Expand All @@ -38,7 +39,7 @@ public class DocumentLoader extends AsyncTaskLoader<Cursor> {
private DocumentsProvider mProvider;
private String mParentDocumentId;

private Intent mPendingAction;
private PendingAction mPendingAction;

public DocumentLoader(Context context, DocumentsProvider provider, String parentDocumentId) {
super(context);
Expand Down Expand Up @@ -76,7 +77,7 @@ public Cursor loadInBackground() {
}
}

public Intent getPendingAction() {
public PendingAction getPendingAction() {
return mPendingAction;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mobilejazz.coltrane.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mobilejazz.coltrane.library.DocumentsProvider;

public class ProviderAdapter extends ArrayAdapter<DocumentsProvider> {

public ProviderAdapter(Context context, int resource) {
super(context, resource);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.root, parent, false);
}

DocumentsProvider provider = getItem(position);
TextView tv = (TextView) convertView;

tv.setText(getContext().getString(R.string.account_name, provider.getName()));
tv.setCompoundDrawablesWithIntrinsicBounds(provider.getIcon(), 0, 0, 0);

return convertView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.mobilejazz.coltrane.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import com.mobilejazz.coltrane.library.DocumentsProvider;
import com.mobilejazz.coltrane.library.DocumentsProviderRegistry;

import java.util.ArrayList;

public class ProviderChooser extends DialogFragment {

public interface OnProviderSelectedListener {

public void onProviderSelected(DocumentsProvider provider);

}

private static final String ARG_PROVIDERS = "com.mobilejazz.coltrane.ui.ProviderChooser.ARG_PROVIDERS";

public static ProviderChooser newInstance(ArrayList<String> providerIds) {
ProviderChooser f = new ProviderChooser();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putStringArrayList(ARG_PROVIDERS, providerIds);
f.setArguments(args);

return f;
}

private ArrayList<String> mProviderIds;
private ProviderAdapter mAdapter;
private OnProviderSelectedListener mListener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() == null) {
setArguments(savedInstanceState);
}

mProviderIds = getArguments().getStringArrayList(ARG_PROVIDERS);

mAdapter = new ProviderAdapter(getActivity(), R.layout.root);
for (String providerId : mProviderIds) {
mAdapter.add(DocumentsProviderRegistry.get().getProvider(providerId));
}
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnProviderSelectedListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement OnProviderSelectedListener");
}
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.choose_account)
.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DocumentsProvider provider = mAdapter.getItem(which);
mListener.onProviderSelected(provider);
dismissAllowingStateLoss();
}
});
return builder.create();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList(ARG_PROVIDERS, mProviderIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/native.lollipop.button.pressed" />
<item android:drawable="@color/native.lollipop.button.normal" />
</selector>
17 changes: 15 additions & 2 deletions document-picker/src/main/res/layout/picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
</LinearLayout>

<!-- The navigation drawer -->
<LinearLayout
<RelativeLayout
android:layout_width="@dimen/drawer.width"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">

<FrameLayout
android:id="@+id/top_label"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
style="?android:attr/actionBarStyle">

<TextView
Expand All @@ -47,14 +49,25 @@

</FrameLayout>

<Button
android:id="@+id/link_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/link_account"
android:onClick="linkAccount"
style="@style/Lollipop.Button.Square" />

<ListView android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top_label"
android:layout_above="@id/link_account"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#f1f1f1" />
</LinearLayout>

</RelativeLayout>

</android.support.v4.widget.DrawerLayout>
4 changes: 4 additions & 0 deletions document-picker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<string name="drawer_open">Open Provider Selection</string>
<string name="drawer_close">Close Provider Selection</string>
<string name="no_provider">No Provider Available</string>
<string name="link_account">Link Account</string>
<string name="account_name">%s Account</string>
<string name="choose_account">Select an account type</string>
<string name="account_linked">Account has been sucessfully linked</string>
</resources>
5 changes: 5 additions & 0 deletions document-picker/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@
<item name="android:textColor">?android:attr/textColorHighlightInverse</item>
</style>

<style name="Lollipop.Button.Square" parent="android:style/Widget.Holo.Button">
<item name="android:background">@drawable/button_background_square</item>
<item name="android:textColor">?android:attr/textColorHighlightInverse</item>
</style>

</resources>
3 changes: 2 additions & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.2'
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
Expand Down Expand Up @@ -41,6 +41,7 @@ dependencies {
compile project(':library')
compile project(':filesystem')
compile project(':gdrive')
compile project(':dropbox')
compile project(':document-picker')
compile 'com.squareup.picasso:picasso:2.4.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import com.mobilejazz.coltrane.provider.dropbox.DropboxProvider;
import com.mobilejazz.coltrane.provider.filesystem.FileSystemProvider;
import com.mobilejazz.coltrane.provider.gdrive.GoogleDriveProvider;

Expand All @@ -21,7 +22,7 @@ public void onCreate() {

FileSystemProvider.register(getApplicationContext());
GoogleDriveProvider.register(getApplicationContext());

DropboxProvider.register(getApplicationContext());
}

/**
Expand Down
Loading

0 comments on commit ac1000a

Please sign in to comment.