|
| 1 | +package com.adobe.assetbrowserui; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.BitmapFactory; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.design.widget.FloatingActionButton; |
| 8 | +import android.support.design.widget.Snackbar; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.support.v7.widget.Toolbar; |
| 11 | +import android.util.Log; |
| 12 | +import android.view.Menu; |
| 13 | +import android.view.MenuItem; |
| 14 | +import android.view.View; |
| 15 | +import android.widget.Button; |
| 16 | +import android.widget.ImageView; |
| 17 | +import android.widget.Toast; |
| 18 | + |
| 19 | +import com.adobe.creativesdk.foundation.auth.AdobeAuthException; |
| 20 | +import com.adobe.creativesdk.foundation.auth.AdobeAuthSessionHelper; |
| 21 | +import com.adobe.creativesdk.foundation.auth.AdobeAuthSessionLauncher; |
| 22 | +import com.adobe.creativesdk.foundation.auth.AdobeUXAuthManager; |
| 23 | +import com.adobe.creativesdk.foundation.internal.utils.AdobeCSDKException; |
| 24 | +import com.adobe.creativesdk.foundation.storage.AdobePhotoAsset; |
| 25 | +import com.adobe.creativesdk.foundation.storage.AdobePhotoAssetRendition; |
| 26 | +import com.adobe.creativesdk.foundation.storage.AdobePhotoException; |
| 27 | +import com.adobe.creativesdk.foundation.storage.AdobeSelection; |
| 28 | +import com.adobe.creativesdk.foundation.storage.AdobeSelectionPhotoAsset; |
| 29 | +import com.adobe.creativesdk.foundation.storage.AdobeUXAssetBrowser; |
| 30 | +import com.adobe.creativesdk.foundation.storage.IAdobeGenericRequestCallback; |
| 31 | + |
| 32 | +import java.io.ByteArrayInputStream; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +public class MainActivity extends AppCompatActivity { |
| 38 | + |
| 39 | + private Button mLaunchAssetBrowserButton; |
| 40 | + private ImageView mSelectedAssetImageView; |
| 41 | + |
| 42 | + private AdobeUXAuthManager mUXAuthManager = AdobeUXAuthManager.getSharedAuthManager(); |
| 43 | + private AdobeAuthSessionHelper mAuthSessionHelper; |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void onCreate(Bundle savedInstanceState) { |
| 47 | + super.onCreate(savedInstanceState); |
| 48 | + setContentView(R.layout.activity_main); |
| 49 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 50 | + setSupportActionBar(toolbar); |
| 51 | + |
| 52 | + mAuthSessionHelper = new AdobeAuthSessionHelper(mStatusCallback); |
| 53 | + mAuthSessionHelper.onCreate(savedInstanceState); |
| 54 | + |
| 55 | + mLaunchAssetBrowserButton = (Button) findViewById(R.id.launchAssetBrowserButton); |
| 56 | + mSelectedAssetImageView = (ImageView) findViewById(R.id.selectedAssetImageView); |
| 57 | + |
| 58 | + View.OnClickListener mLaunchAssetBrowserButtonListener = new View.OnClickListener() { |
| 59 | + @Override |
| 60 | + public void onClick(View v) { |
| 61 | + launchAssetBrowser(); |
| 62 | + } |
| 63 | + }; |
| 64 | + mLaunchAssetBrowserButton.setOnClickListener(mLaunchAssetBrowserButtonListener); |
| 65 | + |
| 66 | + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 67 | + fab.setOnClickListener(new View.OnClickListener() { |
| 68 | + @Override |
| 69 | + public void onClick(View view) { |
| 70 | + Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) |
| 71 | + .setAction("Action", null).show(); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + private void launchAssetBrowser() { |
| 77 | + AdobeUXAssetBrowser assetBrowser = AdobeUXAssetBrowser.getSharedInstance(); |
| 78 | + |
| 79 | + try { |
| 80 | + assetBrowser.popupFileBrowser(this, 300); // Can be any int |
| 81 | + } |
| 82 | + catch (AdobeCSDKException e) { |
| 83 | + Log.e(MainActivity.class.getSimpleName(), "Error: " + e.getMessage()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private AdobeAuthSessionHelper.IAdobeAuthStatusCallback mStatusCallback; |
| 88 | + { |
| 89 | + mStatusCallback = new AdobeAuthSessionHelper.IAdobeAuthStatusCallback() { |
| 90 | + @Override |
| 91 | + public void call(AdobeAuthSessionHelper.AdobeAuthStatus adobeAuthStatus, AdobeAuthException e) { |
| 92 | + if (AdobeAuthSessionHelper.AdobeAuthStatus.AdobeAuthLoggedIn == adobeAuthStatus) { |
| 93 | + /* 3 */ |
| 94 | + showAuthenticatedUI(); |
| 95 | + } else { |
| 96 | + /* 4 */ |
| 97 | + showAdobeLoginUI(); |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + private void showAdobeLoginUI() { |
| 104 | + mUXAuthManager.login(new AdobeAuthSessionLauncher.Builder() |
| 105 | + .withActivity(this) |
| 106 | + .withRequestCode(200) // Can be any int |
| 107 | + .build() |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + private void showAuthenticatedUI() { |
| 112 | + |
| 113 | + /* 5 */ |
| 114 | + Log.i(MainActivity.class.getSimpleName(), "User is logged in!"); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void onResume() { |
| 120 | + super.onResume(); |
| 121 | + mAuthSessionHelper.onResume(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected void onPause() { |
| 126 | + super.onPause(); |
| 127 | + mAuthSessionHelper.onPause(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void onStart() { |
| 132 | + super.onStart(); |
| 133 | + mAuthSessionHelper.onStart(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected void onStop() { |
| 138 | + super.onStop(); |
| 139 | + mAuthSessionHelper.onStop(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected void onDestroy() { |
| 144 | + super.onDestroy(); |
| 145 | + mAuthSessionHelper.onDestroy(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 150 | + super.onActivityResult(requestCode, resultCode, data); |
| 151 | + mAuthSessionHelper.onActivityResult(requestCode, resultCode, data); |
| 152 | + |
| 153 | + if (data != null && resultCode == RESULT_OK) { |
| 154 | + switch (requestCode) { |
| 155 | + case 300: // The request code we used in launchAssetBrowser() |
| 156 | + |
| 157 | + /* 1) */ |
| 158 | + AdobeUXAssetBrowser.ResultProvider assetBrowserResult = new AdobeUXAssetBrowser.ResultProvider(data); |
| 159 | + ArrayList listOfSelectedAssetFiles = assetBrowserResult.getSelectionAssetArray(); |
| 160 | + AdobeSelection selection = (AdobeSelection) listOfSelectedAssetFiles.get(0); |
| 161 | + |
| 162 | + /* 2) */ |
| 163 | + if (selection instanceof AdobeSelectionPhotoAsset) { |
| 164 | + |
| 165 | + /* 3) */ |
| 166 | + IAdobeGenericRequestCallback<byte[], AdobePhotoException> downloadCallBack = new IAdobeGenericRequestCallback<byte[], AdobePhotoException>() { |
| 167 | + @Override |
| 168 | + public void onCancellation() { |
| 169 | + /* 3.a) Cancellation code here */ |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void onCompletion(byte[] bytes) { |
| 174 | + |
| 175 | + /* 3.b) */ |
| 176 | + InputStream inputStream = new ByteArrayInputStream(bytes); |
| 177 | + Bitmap image = BitmapFactory.decodeStream(inputStream); |
| 178 | + mSelectedAssetImageView.setImageBitmap(image); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public void onError(AdobePhotoException e) { |
| 183 | + /* 3.c) Error handler here */ |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void onProgress(double v) { |
| 188 | + /* 3.d) Code for indicating download progress here */ |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + /* 4) */ |
| 193 | + AdobePhotoAsset photoAsset = ((AdobeSelectionPhotoAsset) selection).getSelectedItem(); |
| 194 | + Map<String, AdobePhotoAssetRendition> renditionMap = photoAsset.getRenditions(); |
| 195 | + photoAsset.downloadRendition(renditionMap.get(AdobePhotoAsset.AdobePhotoAssetRenditionImage2048), downloadCallBack); |
| 196 | + } |
| 197 | + |
| 198 | + else { |
| 199 | + Toast.makeText(MainActivity.this, "Please choose a Lightroom Photo", Toast.LENGTH_LONG).show(); |
| 200 | + } |
| 201 | + |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 209 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 210 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 211 | + return true; |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 216 | + // Handle action bar item clicks here. The action bar will |
| 217 | + // automatically handle clicks on the Home/Up button, so long |
| 218 | + // as you specify a parent activity in AndroidManifest.xml. |
| 219 | + int id = item.getItemId(); |
| 220 | + |
| 221 | + //noinspection SimplifiableIfStatement |
| 222 | + if (id == R.id.action_logout) { |
| 223 | + mUXAuthManager.logout(); |
| 224 | + return true; |
| 225 | + } |
| 226 | + |
| 227 | + return super.onOptionsItemSelected(item); |
| 228 | + } |
| 229 | +} |
0 commit comments