|
| 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