Skip to content

Commit 67861b3

Browse files
authored
Fix sample app
1 parent d122d24 commit 67861b3

File tree

11 files changed

+57
-51
lines changed

11 files changed

+57
-51
lines changed

preprocess/src/main/java/com/cloudinary/android/preprocess/BitmapEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.Bitmap;
5+
import android.os.Build;
56

67
import com.cloudinary.android.preprocess.ResourceCreationException;
78
import com.cloudinary.android.preprocess.ResourceEncoder;
@@ -80,9 +81,13 @@ protected final String saveFile(Context context, Bitmap resource, int quality, F
8081
return file;
8182
}
8283

84+
@SuppressWarnings("deprecation")
8385
private Bitmap.CompressFormat adaptFormat(Format format) {
8486
switch (format) {
8587
case WEBP:
88+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
89+
return Bitmap.CompressFormat.WEBP_LOSSY;
90+
}
8691
return Bitmap.CompressFormat.WEBP;
8792
case JPEG:
8893
return Bitmap.CompressFormat.JPEG;

preprocess/src/main/java/com/cloudinary/android/preprocess/Transcode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.linkedin.android.litr.MediaTransformer;
1616
import com.linkedin.android.litr.TransformationListener;
17+
import com.linkedin.android.litr.TransformationOptions;
1718
import com.linkedin.android.litr.analytics.TrackTransformationInfo;
1819

1920
import java.io.File;
@@ -55,14 +56,14 @@ public Uri execute(Context context, Uri resource) throws PreprocessException {
5556

5657
TransformationListener transformationListener = new VideoTransformationListener();
5758
MediaTransformer mediaTransformer = new MediaTransformer(context.getApplicationContext());
59+
TransformationOptions options = new TransformationOptions.Builder().setGranularity(MediaTransformer.GRANULARITY_DEFAULT).build();
5860
mediaTransformer.transform(parameters.getRequestId(),
5961
resource,
6062
targetFilePath,
6163
targetVideoFormat,
6264
targetAudioFormat,
6365
transformationListener,
64-
MediaTransformer.GRANULARITY_DEFAULT,
65-
null);
66+
options);
6667

6768
synchronized (lockObject) {
6869
try {

sample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ android {
3535

3636
dependencies {
3737
implementation project(':all')
38+
implementation 'androidx.preference:preference:1.2.0'
3839
implementation 'com.android.support:multidex:1.0.3'
3940
implementation 'androidx.appcompat:appcompat:1.3.0'
4041
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'

sample/src/main/java/com/cloudinary/android/sample/app/CloudinaryService.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import android.os.HandlerThread;
1313
import android.os.IBinder;
1414
import androidx.annotation.Nullable;
15+
import androidx.core.app.NotificationCompat;
16+
import androidx.core.content.ContextCompat;
1517
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
1618
import android.util.TypedValue;
1719

@@ -40,14 +42,14 @@ public class CloudinaryService extends ListenerService {
4042
private NotificationManager notificationManager;
4143
private AtomicInteger idsProvider = new AtomicInteger(1000);
4244
private Map<String, Integer> requestIdsToNotificationIds = new ConcurrentHashMap<>();
43-
private Notification.Builder builder;
45+
private NotificationCompat.Builder builder;
4446
private Handler backgroundThreadHandler;
4547

4648
@Override
4749
public void onCreate() {
4850
super.onCreate();
4951
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
50-
builder = new Notification.Builder(this);
52+
builder = new NotificationCompat.Builder(this, MainApplication.NOTIFICATION_CHANNEL_ID);
5153
builder.setContentTitle("Uploading to Cloudinary...")
5254
.setSmallIcon(R.drawable.ic_launcher)
5355
.setContentIntent(PendingIntent.getActivity(this, 999,
@@ -72,8 +74,8 @@ public IBinder onBind(Intent intent) {
7274
}
7375

7476

75-
private Notification.Builder getBuilder(String requestId, Resource.UploadStatus status) {
76-
Notification.Builder builder = new Notification.Builder(this)
77+
private NotificationCompat.Builder getBuilder(String requestId, Resource.UploadStatus status) {
78+
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, MainApplication.NOTIFICATION_CHANNEL_ID)
7779
.setSmallIcon(R.drawable.ic_launcher)
7880
.setAutoCancel(true)
7981
.setContentIntent(PendingIntent.getActivity(this, 1234,
@@ -87,7 +89,7 @@ private Notification.Builder getBuilder(String requestId, Resource.UploadStatus
8789
}
8890

8991
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
90-
builder.setColor(getResources().getColor(R.color.colorPrimary));
92+
builder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
9193
}
9294

9395
return builder;
@@ -151,7 +153,7 @@ private void cancelNotification(String requestId) {
151153
notificationManager.cancel(id);
152154
}
153155
}
154-
156+
@SuppressWarnings("deprecation")
155157
private boolean sendBroadcast(Resource updatedResource) {
156158
// This is called from background threads and the main thread may touch the resource and delete it
157159
// in the meantime (from the activity) - verify it's still around before sending the broadcast
@@ -179,6 +181,7 @@ public void run() {
179181
.build());
180182
}
181183

184+
@SuppressWarnings("deprecation")
182185
@Override
183186
public synchronized void onProgress(String requestId, long bytes, long totalBytes) {
184187
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_UPLOAD_PROGRESS).putExtra("requestId", requestId).putExtra("bytes", bytes).putExtra("totalBytes", totalBytes));
@@ -257,7 +260,7 @@ public void run() {
257260
getBuilder(requestId, Resource.UploadStatus.FAILED)
258261
.setContentTitle("Error uploading.")
259262
.setContentText(error.getDescription())
260-
.setStyle(new Notification.BigTextStyle()
263+
.setStyle(new NotificationCompat.BigTextStyle()
261264
.setBigContentTitle("Error uploading.")
262265
.bigText(error.getDescription()))
263266
.build());

sample/src/main/java/com/cloudinary/android/sample/app/ImageActivity.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.cloudinary.android.sample.app;
22

3-
import android.annotation.SuppressLint;
43
import android.content.ClipData;
54
import android.content.Intent;
65
import android.net.Uri;
76
import android.os.Bundle;
8-
import android.os.Handler;
97
import android.view.Menu;
108
import android.view.MenuInflater;
119
import android.view.MenuItem;
@@ -34,18 +32,15 @@
3432
import com.google.android.exoplayer2.ExoPlayer;
3533
import com.google.android.exoplayer2.MediaItem;
3634
import com.google.android.exoplayer2.PlaybackException;
37-
import com.google.android.exoplayer2.PlaybackParameters;
38-
import com.google.android.exoplayer2.SimpleExoPlayer;
35+
import com.google.android.exoplayer2.Player;
36+
import com.google.android.exoplayer2.TracksInfo;
3937
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
4038
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
4139
import com.google.android.exoplayer2.source.MediaSource;
4240
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
43-
import com.google.android.exoplayer2.source.TrackGroupArray;
44-
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
4541
import com.google.android.exoplayer2.ui.PlayerView;
4642
import com.google.android.exoplayer2.upstream.DataSource;
47-
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
48-
import com.google.android.exoplayer2.util.Util;
43+
import com.google.android.exoplayer2.upstream.DefaultDataSource;
4944
import com.google.android.material.snackbar.Snackbar;
5045

5146
import java.util.List;
@@ -61,9 +56,9 @@ public class ImageActivity extends AppCompatActivity {
6156
private int thumbHeight;
6257
private TextView descriptionTextView;
6358
private ProgressBar progressBar;
64-
private SimpleExoPlayer exoPlayer;
59+
private ExoPlayer exoPlayer;
6560
private PlayerView exoPlayerView;
66-
private ExoPlayer.EventListener listener;
61+
private Player.Listener listener;
6762
private String currentUrl = null;
6863

6964
@Override
@@ -96,37 +91,31 @@ public boolean onCreateOptionsMenu(Menu menu) {
9691

9792

9893
private void initExoPlayer() {
99-
exoPlayer = new SimpleExoPlayer.Builder(this).build();
94+
exoPlayer = new ExoPlayer.Builder(this).build();
10095
exoPlayerView = ((PlayerView) findViewById(R.id.exoPlayer));
10196
exoPlayerView.setPlayer(exoPlayer);
10297

103-
listener = new ExoPlayer.EventListener() {
98+
listener = new Player.Listener() {
10499

105100
@Override
106-
public void onTracksChanged(TrackGroupArray trackGroupArray, TrackSelectionArray trackSelectionArray) {
107-
if (trackGroupArray.length > 0) {
101+
public void onTracksInfoChanged(TracksInfo tracksInfo) {
102+
Player.Listener.super.onTracksInfoChanged(tracksInfo);
103+
if (tracksInfo.getTrackGroupInfos().isEmpty()) {
108104
progressBar.setVisibility(View.GONE);
109105
}
110106
}
111107

112108
@Override
113-
public void onLoadingChanged(boolean b) {
114-
progressBar.setVisibility(b ? View.VISIBLE : View.GONE);
115-
}
116-
117-
@Override
118-
public void onPlayerStateChanged(boolean b, int i) {
119-
109+
public void onIsLoadingChanged(boolean isLoading) {
110+
Player.Listener.super.onIsLoadingChanged(isLoading);
111+
progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
120112
}
121113

122114
@Override
123115
public void onPlayerError(PlaybackException error) {
116+
Player.Listener.super.onPlayerError(error);
124117
progressBar.setVisibility(View.GONE);
125118
Toast.makeText(ImageActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
126-
}
127-
128-
@Override
129-
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
130119

131120
}
132121
};
@@ -174,7 +163,7 @@ private void updateMainImage(EffectData data) {
174163
private void loadVideo(final EffectData data) {
175164
progressBar.setVisibility(View.VISIBLE);
176165
imageView.setVisibility(View.GONE);
177-
final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Cloudinary Sample App"), null);
166+
final DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
178167
final ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
179168
Url baseUrl = MediaManager.get().url().secure(true).resourceType("video").publicId(data.getPublicId()).transformation(data.getTransformation());
180169
MediaManager.get().responsiveUrl(exoPlayerView, baseUrl, FIT, new ResponsiveUrl.Callback() {

sample/src/main/java/com/cloudinary/android/sample/app/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cloudinary.android.sample.app;
22

3-
import android.annotation.SuppressLint;
43
import android.content.BroadcastReceiver;
54
import android.content.ClipData;
65
import android.content.Context;
@@ -25,7 +24,6 @@
2524
import androidx.fragment.app.FragmentPagerAdapter;
2625
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2726
import androidx.recyclerview.widget.RecyclerView;
28-
import androidx.viewpager.widget.PagerAdapter;
2927
import androidx.viewpager.widget.ViewPager;
3028

3129
import com.cloudinary.android.MediaManager;
@@ -42,6 +40,7 @@
4240
import java.util.ArrayList;
4341
import java.util.List;
4442

43+
@SuppressWarnings("deprecation")
4544
public class MainActivity extends AppCompatActivity implements ResourcesAdapter.ImageClickedListener, DeleteRequestsCallback {
4645
public static final int PAGE_COUNT = 4;
4746
public static final int UPLOADED_PAGE_POSITION = 0;

sample/src/main/java/com/cloudinary/android/sample/app/ResourcesAdapter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cloudinary.android.sample.app;
22

3+
import android.os.Build;
34
import android.text.SpannableString;
45
import android.text.style.ForegroundColorSpan;
56
import android.view.LayoutInflater;
@@ -235,6 +236,7 @@ public void onUrlReady(Url url) {
235236
}
236237
}
237238

239+
@SuppressWarnings("deprecation")
238240
private void setProgress(ResourceViewHolder holder, ResourceWithMeta resourceWithMeta) {
239241
holder.progressBar.setVisibility(View.VISIBLE);
240242
((ViewGroup)holder.progressBar.getParent()).findViewById(R.id.progressContainer).setVisibility(View.VISIBLE);
@@ -261,7 +263,13 @@ private void setProgress(ResourceViewHolder holder, ResourceWithMeta resourceWit
261263

262264
String text = holder.statusText.getContext().getString(R.string.uploading, progressStr);
263265
SpannableString spannableString = new SpannableString(text);
264-
spannableString.setSpan(new ForegroundColorSpan(holder.statusText.getContext().getResources().getColor(R.color.buttonColor)), text.indexOf(progressStr), text.length(), 0);
266+
ForegroundColorSpan color;
267+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
268+
color = new ForegroundColorSpan(holder.statusText.getContext().getColor(R.color.buttonColor));
269+
} else {
270+
color = new ForegroundColorSpan(holder.statusText.getContext().getResources().getColor(R.color.buttonColor));
271+
}
272+
spannableString.setSpan(color, text.indexOf(progressStr), text.length(), 0);
265273
holder.statusText.setText(spannableString);
266274
}
267275

sample/src/main/java/com/cloudinary/android/sample/app/Utils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.graphics.PorterDuffXfermode;
1717
import android.graphics.Rect;
1818
import android.net.Uri;
19+
import android.os.Build;
1920
import android.provider.DocumentsContract;
2021
import androidx.core.util.Pair;
2122
import android.view.WindowManager;
@@ -94,11 +95,15 @@ public static Uri toUri(Context context, int resourceId) {
9495
+ '/' + res.getResourceTypeName(resourceId) + '/' + res.getResourceEntryName(resourceId));
9596
}
9697

98+
@SuppressWarnings("deprecation")
9799
public static int getScreenWidth(Context context) {
98100
WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
99101
Point point = new Point();
100-
window.getDefaultDisplay().getSize(point);
101-
return point.x;
102+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
103+
window.getDefaultDisplay().getSize(point);
104+
return point.x;
105+
}
106+
return window.getCurrentWindowMetrics().getBounds().width();
102107
}
103108

104109
@SuppressLint("Range")

sample/src/main/java/com/cloudinary/android/sample/persist/Prefs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.cloudinary.android.sample.persist;
22

33
import android.content.SharedPreferences;
4-
import android.preference.PreferenceManager;
4+
5+
import androidx.preference.PreferenceManager;
56

67
import com.cloudinary.android.sample.app.MainApplication;
78

ui/src/main/java/com/cloudinary/android/uploadwidget/ui/CropRotateFragment.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,11 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
147147
return false;
148148
}
149149
});
150-
150+
setToolbarIfNeeded();
151151
return view;
152152
}
153153

154-
@Override
155-
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
156-
super.onActivityCreated(savedInstanceState);
157-
154+
private void setToolbarIfNeeded() {
158155
AppCompatActivity activity = (AppCompatActivity) getActivity();
159156
if (activity != null) {
160157
Toolbar toolbar = getActivity().findViewById(R.id.cropRotateToolbar);

0 commit comments

Comments
 (0)