Skip to content

Commit 5f47303

Browse files
committed
Added Openwith code and example in android for 3.0.1 release
1 parent c409404 commit 5f47303

File tree

14 files changed

+901
-89
lines changed

14 files changed

+901
-89
lines changed

ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
3.0.1 (2017-03-29)
2+
---------------------------------------------
3+
- Add OpenWith support for official partners.
4+
15
3.0.0 (2017-03-17)
26
---------------------------------------------
37
- Breaking changes:

ReadMe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ If you're using Maven, then edit your project's "pom.xml" and add this to the `<
1414
<dependency>
1515
<groupId>com.dropbox.core</groupId>
1616
<artifactId>dropbox-core-sdk</artifactId>
17-
<version>3.0.0</version>
17+
<version>3.0.1</version>
1818
</dependency>
1919
```
2020

@@ -23,7 +23,7 @@ If you are using Gradle, then edit your project's "build.gradle" and add this to
2323
```groovy
2424
dependencies {
2525
// ...
26-
compile 'com.dropbox.core:dropbox-core-sdk:3.0.0'
26+
compile 'com.dropbox.core:dropbox-core-sdk:3.0.1'
2727
}
2828
```
2929

examples/android/src/main/AndroidManifest.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212
android:label="@string/app_name"
1313
android:theme="@style/AppTheme"
1414
android:supportsRtl="false">
15-
1615
<activity
17-
android:name="com.dropbox.core.examples.android.UserActivity"
18-
android:label="@string/app_name" >
16+
android:name=".UserActivity"
17+
android:label="@string/app_name">
1918
<intent-filter>
2019
<action android:name="android.intent.action.MAIN" />
2120
<category android:name="android.intent.category.LAUNCHER" />
2221
</intent-filter>
2322
</activity>
2423
<activity
25-
android:name="com.dropbox.core.examples.android.FilesActivity"
26-
android:label="@string/title_activity_files" >
27-
</activity>
28-
24+
android:name=".FilesActivity"
25+
android:label="@string/title_activity_files" />
2926
<activity
3027
android:name="com.dropbox.core.android.AuthActivity"
3128
android:configChanges="orientation|keyboard"
32-
android:launchMode="singleTask" >
29+
android:launchMode="singleTask">
3330
<intent-filter>
3431

3532
<!-- Change this to be db- followed by your app key -->
@@ -41,6 +38,15 @@
4138
<category android:name="android.intent.category.DEFAULT" />
4239
</intent-filter>
4340
</activity>
41+
<activity
42+
android:name=".internal.OpenWithActivity"
43+
android:label="@string/title_activity_openwith">
44+
<intent-filter>
45+
<action android:name="com.dropbox.android.intent.action.DBXC_EDIT"/>
46+
<category android:name="android.intent.category.DEFAULT"/>
47+
<data android:mimeType="text/plain"/>
48+
</intent-filter>
49+
</activity>
4450
</application>
4551

46-
</manifest>
52+
</manifest>

examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ protected void onResume() {
2727
} else {
2828
initAndLoadData(accessToken);
2929
}
30+
31+
String uid = Auth.getUid();
32+
String storedUid = prefs.getString("user-id", null);
33+
if (uid != null && !uid.equals(storedUid)) {
34+
prefs.edit().putString("user-id", uid).apply();
35+
}
3036
}
3137

3238
private void initAndLoadData(String accessToken) {

examples/android/src/main/java/com/dropbox/core/examples/android/UserActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.dropbox.core.examples.android;
22

3+
import android.content.Intent;
4+
import android.content.SharedPreferences;
35
import android.os.Bundle;
46
import android.support.v7.widget.Toolbar;
57
import android.util.Log;
@@ -8,6 +10,7 @@
810
import android.widget.TextView;
911

1012
import com.dropbox.core.android.Auth;
13+
import com.dropbox.core.examples.android.internal.OpenWithActivity;
1114
import com.dropbox.core.v2.users.FullAccount;
1215

1316

@@ -41,6 +44,15 @@ public void onClick(View v) {
4144
startActivity(FilesActivity.getIntent(UserActivity.this, ""));
4245
}
4346
});
47+
48+
Button openWithButton = (Button)findViewById(R.id.open_with);
49+
openWithButton.setOnClickListener(new View.OnClickListener() {
50+
@Override
51+
public void onClick(View v) {
52+
Intent openWithIntent = new Intent(UserActivity.this, OpenWithActivity.class);
53+
startActivity(openWithIntent);
54+
}
55+
});
4456
}
4557

4658
@Override
@@ -53,12 +65,14 @@ protected void onResume() {
5365
findViewById(R.id.name_text).setVisibility(View.VISIBLE);
5466
findViewById(R.id.type_text).setVisibility(View.VISIBLE);
5567
findViewById(R.id.files_button).setEnabled(true);
68+
findViewById(R.id.open_with).setEnabled(true);
5669
} else {
5770
findViewById(R.id.login_button).setVisibility(View.VISIBLE);
5871
findViewById(R.id.email_text).setVisibility(View.GONE);
5972
findViewById(R.id.name_text).setVisibility(View.GONE);
6073
findViewById(R.id.type_text).setVisibility(View.GONE);
6174
findViewById(R.id.files_button).setEnabled(false);
75+
findViewById(R.id.open_with).setEnabled(false);
6276
}
6377
}
6478

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package com.dropbox.core.examples.android.internal;
2+
3+
import android.content.Intent;
4+
import android.content.SharedPreferences;
5+
import android.net.Uri;
6+
import android.os.Bundle;
7+
import android.os.Parcel;
8+
import android.support.v7.widget.Toolbar;
9+
import android.util.Base64;
10+
import android.view.View;
11+
import android.widget.Button;
12+
import android.widget.EditText;
13+
import android.widget.Toast;
14+
15+
import com.dropbox.core.android.Auth;
16+
import com.dropbox.core.android.DbxOfficialAppConnector;
17+
import com.dropbox.core.android.DropboxParseException;
18+
import com.dropbox.core.android.DropboxUidNotInitializedException;
19+
import com.dropbox.core.examples.android.DropboxActivity;
20+
import com.dropbox.core.examples.android.R;
21+
22+
/**
23+
* This example is only for 3rd party apps who registered OpenWith feature at our server side who use
24+
* intent action {@value DbxOfficialAppConnector#ACTION_DBXC_EDIT} and
25+
* {@value DbxOfficialAppConnector#ACTION_DBXC_VIEW} to jump to their apps. Don't follow this if
26+
* you don't need openwith or if you use regular {@value Intent#ACTION_EDIT} and
27+
* {@value Intent#ACTION_VIEW}.
28+
*/
29+
public class OpenWithActivity extends DropboxActivity {
30+
private DbxOfficialAppConnector mDoac;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_open_with);
36+
37+
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
38+
setSupportActionBar(toolbar);
39+
40+
Button generateIntentButton = (Button)findViewById(R.id.generate_intent);
41+
generateIntentButton.setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View v) {
44+
EditText editText = (EditText) findViewById(R.id.editText);
45+
String path = editText.getText().toString();
46+
//fake OpenWithIntent with some dummy parameters
47+
Intent fakeOpenWithIntent = generateOpenWithIntent(path);
48+
//encode the fake OpenWithIntent into UtmContent
49+
String encodedFakeIntent = encodeOpenWithIntent(fakeOpenWithIntent);
50+
try {
51+
//test that decoding utmcontent works
52+
Intent decodedIntent = DbxOfficialAppConnector
53+
.generateOpenWithIntentFromUtmContent(encodedFakeIntent);
54+
//start that fake OpenWithIntent. This will lead us to a new OpenWithActivity.
55+
startActivity(decodedIntent);
56+
} catch (DropboxParseException e) {
57+
// TODO Auto-generated catch block
58+
e.printStackTrace();
59+
}
60+
}
61+
});
62+
63+
Button mInstalled = (Button)findViewById(R.id.is_installed);
64+
mInstalled.setOnClickListener(new View.OnClickListener() {
65+
@Override
66+
public void onClick(View v) {
67+
DbxOfficialAppConnector.DbxOfficialAppInstallInfo installInfo =
68+
DbxOfficialAppConnector.isInstalled(OpenWithActivity.this);
69+
showToast((installInfo != null)?installInfo.toString():"Not installed!");
70+
}
71+
});
72+
73+
Button mGenLinked = (Button)findViewById(R.id.is_linked_any_button);
74+
mGenLinked.setOnClickListener(new View.OnClickListener() {
75+
@Override
76+
public void onClick(View v) {
77+
boolean isSigned = DbxOfficialAppConnector.isAnySignedIn(OpenWithActivity.this);
78+
showToast("Any Signed in?:" + isSigned);
79+
}
80+
});
81+
82+
Button mSpecLinked = (Button)findViewById(R.id.is_linked_spec_button);
83+
mSpecLinked.setOnClickListener(new View.OnClickListener() {
84+
@Override
85+
public void onClick(View v) {
86+
boolean isSigned = mDoac.isSignedIn(OpenWithActivity.this);
87+
showToast("Signed in?:" + isSigned);
88+
}
89+
});
90+
91+
Button mPreview = (Button)findViewById(R.id.preview_button);
92+
mPreview.setOnClickListener(new View.OnClickListener() {
93+
@Override
94+
public void onClick(View v) {
95+
EditText editText = (EditText) findViewById(R.id.editText);
96+
String path = editText.getText().toString();
97+
Intent pIntent = mDoac.getPreviewFileIntent(OpenWithActivity.this, path, "");
98+
startActivity(pIntent);
99+
}
100+
});
101+
102+
Button mUpgrade = (Button)findViewById(R.id.upgrade_button);
103+
mUpgrade.setOnClickListener(new View.OnClickListener() {
104+
@Override
105+
public void onClick(View v) {
106+
Intent uIntent = mDoac.getUpgradeAccountIntent(OpenWithActivity.this);
107+
startActivity(uIntent);
108+
}
109+
});
110+
111+
}
112+
113+
/* Because it's just a fake intent, we just print it in Toast and do nothing here. As third
114+
* party app you should take the intent from Dropbox official app and potentially go through
115+
* the auth flow. Finally handle that file sent with this intent.
116+
*/
117+
private void handleFakeOpenWithIntent(Intent intent) {
118+
if (intent.getAction() == DbxOfficialAppConnector.ACTION_DBXC_EDIT ||
119+
intent.getAction() == DbxOfficialAppConnector.ACTION_DBXC_VIEW) {
120+
String path = intent.getStringExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_PATH);
121+
String uid = intent.getStringExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_UID);
122+
boolean read_only = intent.getBooleanExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_READ_ONLY, false);
123+
String session_id = intent.getStringExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_SESSION_ID);
124+
showToast(path + uid + read_only + session_id);
125+
}
126+
}
127+
128+
protected Intent generateOpenWithIntent(String path) {
129+
/*
130+
* Generate an OpenWithIntent.
131+
* Real 3rd party apps should never run this function as DropboxApp does this entirely
132+
*/
133+
String uid = Auth.getUid();
134+
135+
// fake the URI
136+
// WARNING: URI FORMAT IS NOT FINALIZED AND MAY CHANGE AT ANY TIME
137+
Uri.Builder builder = new Uri.Builder();
138+
builder.scheme("content");
139+
builder.authority("com.dropbox.android.FileCache");
140+
builder.appendPath("filecache");
141+
builder.appendPath(uid);
142+
143+
for (String component : path.substring(1).split("/")) {
144+
builder.appendPath(component);
145+
}
146+
Uri uri = builder.build();
147+
// end URI fakery
148+
149+
Intent owpIntent = new Intent(DbxOfficialAppConnector.ACTION_DBXC_EDIT, uri);
150+
151+
// extras
152+
owpIntent.putExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_PATH, path);
153+
owpIntent.putExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_UID, uid);
154+
owpIntent.putExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_READ_ONLY, false);
155+
owpIntent.putExtra(DbxOfficialAppConnector.EXTRA_DROPBOX_SESSION_ID, "generated");
156+
157+
owpIntent.setDataAndType(uri, "text/plain");
158+
159+
return owpIntent;
160+
}
161+
162+
protected String encodeOpenWithIntent(Intent owpIntent) {
163+
/*
164+
* Encode OpenWith intent
165+
* Real 3rd party apps should never run this function as DropboxApp does this entirely
166+
*/
167+
168+
Bundle extras = owpIntent.getExtras();
169+
extras.putString("_action", owpIntent.getAction());
170+
extras.putParcelable("_uri", owpIntent.getData());
171+
extras.putString("_type", owpIntent.getType());
172+
173+
// marshall it!
174+
final Parcel parcel = Parcel.obtain();
175+
parcel.writeBundle(extras);
176+
byte[] b = parcel.marshall();
177+
parcel.recycle();
178+
return new String(Base64.encode(b, 0));
179+
}
180+
181+
private void showToast(String msg) {
182+
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
183+
error.show();
184+
}
185+
186+
@Override
187+
protected void loadData() {
188+
try {
189+
String uid = Auth.getUid();
190+
if (uid == null) {
191+
SharedPreferences prefs = getSharedPreferences("dropbox-sample", MODE_PRIVATE);
192+
uid = prefs.getString("user-id", null);
193+
}
194+
this.mDoac = new DbxOfficialAppConnector(uid);
195+
} catch (DropboxUidNotInitializedException e) {
196+
e.printStackTrace();
197+
return;
198+
}
199+
200+
handleFakeOpenWithIntent(getIntent());
201+
}
202+
}

0 commit comments

Comments
 (0)