Skip to content

Commit

Permalink
Add feature ATTACHMENT_RESIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Feb 22, 2017
1 parent 6f425b6 commit dd25c58
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 67 deletions.
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.volley:volley:1.0.0'
compile 'net.gotev:uploadservice:3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.bumptech.glide:volley-integration:1.4.0@aar'
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile('com.twitter.sdk.android:twitter:1.6.0@aar') {
transitive = true;
}
compile 'org.apache.httpcomponents:httpcore:4.3.2'
compile 'org.apache.httpcomponents:httpmime:4.3.5'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:design:25.1.1'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.google.android.gms:play-services:10.2.0'
}

Expand Down
1 change: 1 addition & 0 deletions app/config.gradle.default
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ android {

buildConfigField "int", "FEATURE_CONFIRM_SIGN_IN_WITH_REMEMBER", "1"
buildConfigField "String", "FEATURE_DEFAULT_URL", "\"\""
buildConfigField "int", "FEATURE_ATTACHMENT_RESIZE", "2048"
}

}
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# https://github.com/bumptech/glide/wiki/Integration-Libraries#generic-proguard
-keep public class * implements com.bumptech.glide.module.GlideModule
35 changes: 4 additions & 31 deletions app/src/main/java/com/xfrocks/api/androiddemo/App.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.xfrocks.api.androiddemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.xfrocks.api.androiddemo.helper.PubKeyManager;

Expand All @@ -27,7 +25,6 @@ public class App extends MultiDexApplication {
private static App sInstance;

private RequestQueue mRequestQueue;
private ImageLoader mNetworkImageLoader;

@Override
public void onCreate() {
Expand Down Expand Up @@ -61,34 +58,6 @@ public RequestQueue getRequestQueue() {
return mRequestQueue;
}

public ImageLoader getNetworkImageLoader() {
if (mNetworkImageLoader == null) {
// create a network image loader with a super simple image cache
// which only keep cached data of one bitmap for one url at a time
mNetworkImageLoader = new ImageLoader(getRequestQueue(), new ImageLoader.ImageCache() {
String mUrl;
Bitmap mBitmap;

@Override
public Bitmap getBitmap(String url) {
if (url.equals(mUrl)) {
return mBitmap;
}

return null;
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
mUrl = url;
mBitmap = bitmap;
}
});
}

return mNetworkImageLoader;
}

public synchronized static App getInstance() {
return sInstance;
}
Expand All @@ -100,4 +69,8 @@ public static Uri getTempForCamera(Context context) {
public static boolean getFeatureConfirmSignInWithRemember() {
return BuildConfig.FEATURE_CONFIRM_SIGN_IN_WITH_REMEMBER > 0;
}

public static int getFeatureAttachmentResize() {
return BuildConfig.FEATURE_ATTACHMENT_RESIZE;
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/xfrocks/api/androiddemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.xfrocks.api.androiddemo.helper.ChooserIntent;
import com.xfrocks.api.androiddemo.persist.ObjectAsFile;
import com.xfrocks.api.androiddemo.persist.Row;
Expand Down Expand Up @@ -286,10 +286,10 @@ private void setUser(Api.User u) {
mUser = u;

if (mUser != null) {
App.getInstance().getNetworkImageLoader().get(
mUser.getAvatar(),
ImageLoader.getImageListener(mHeaderImg, R.drawable.avatar_l, 0)
);
Glide.with(this)
.load(mUser.getAvatar())
.placeholder(R.drawable.avatar_l)
.into(mHeaderImg);
mHeaderTxt.setText(mUser.getUsername());
} else {
mHeaderImg.setImageDrawable(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.xfrocks.api.androiddemo.Api;
import com.xfrocks.api.androiddemo.App;
import com.xfrocks.api.androiddemo.LoginActivity;
import com.xfrocks.api.androiddemo.R;
import com.xfrocks.api.androiddemo.persist.ObjectAsFile;
Expand Down Expand Up @@ -544,10 +543,10 @@ public void onBindViewHolder(ViewHolder holder, int position) {
|| !message.getCreatorUserId().equals(messageNext.getCreatorUserId())) {
holder.avatar.setVisibility(View.VISIBLE);
holder.avatar.setContentDescription(message.getCreatorName());
App.getInstance().getNetworkImageLoader().get(
message.getCreatorAvatar(),
ImageLoader.getImageListener(holder.avatar, R.drawable.avatar_l, 0)
);
Glide.with(DiscussionActivity.this)
.load(message.getCreatorAvatar())
.placeholder(R.drawable.avatar_l)
.into(holder.avatar);
} else {
holder.avatar.setVisibility(View.GONE);
}
Expand Down Expand Up @@ -596,10 +595,10 @@ public void onBindViewHolder(ViewHolder holder, int position) {

attachmentImageView.setContentDescription(getString(R.string.attachment_id_x_name_y,
attachment.getAttachmentId(), attachment.getFileName()));
App.getInstance().getNetworkImageLoader().get(
attachment.getThumbnail(),
ImageLoader.getImageListener(attachmentImageView, R.drawable.avatar_l, 0)
);
Glide.with(DiscussionActivity.this)
.load(attachment.getThumbnail())
.placeholder(R.drawable.avatar_l)
.into(attachmentImageView);

attachmentImageView.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.xfrocks.api.androiddemo.Api;
import com.xfrocks.api.androiddemo.App;
import com.xfrocks.api.androiddemo.LoginActivity;
import com.xfrocks.api.androiddemo.R;

Expand Down Expand Up @@ -301,10 +300,10 @@ public void onBindViewHolder(ViewHolder holder, int position) {
Api.Discussion discussion = mData.get(position);

holder.avatar.setContentDescription(discussion.getCreatorName());
App.getInstance().getNetworkImageLoader().get(
discussion.getCreatorAvatar(),
ImageLoader.getImageListener(holder.avatar, R.drawable.avatar_l, 0)
);
Glide.with(DiscussionListActivity.this)
.load(discussion.getCreatorAvatar())
.placeholder(R.drawable.avatar_l)
.into(holder.avatar);

holder.title.setText(discussion.getTitle());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -20,7 +22,11 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.xfrocks.api.androiddemo.Api;
import com.xfrocks.api.androiddemo.App;
import com.xfrocks.api.androiddemo.R;
import com.xfrocks.api.androiddemo.helper.ChooserIntent;

Expand All @@ -30,7 +36,10 @@
import net.gotev.uploadservice.UploadNotificationConfig;
import net.gotev.uploadservice.UploadStatusDelegate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

Expand Down Expand Up @@ -213,22 +222,86 @@ void attemptCamera() {
startActivityForResult(cameraIntents[0], RC_PICK_FILE);
}

void uploadAttach(Uri uri) {
void attemptResize(final Uri uri, int size) {
Glide.with(getActivity().getApplicationContext())
.load(uri)
.asBitmap()
.override(size, size)
.fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
onImageResized(uri, resource);
}
});
}

void onImageResized(Uri uri, Bitmap resource) {
String fileName = ChooserIntent.getFileNameFromUri(getContext(), uri);
String prefix = fileName;
String suffix = null;
int indexOfDot = fileName.lastIndexOf(".");
if (indexOfDot > -1) {
prefix = fileName.substring(0, indexOfDot);
suffix = fileName.substring(indexOfDot + 1).toLowerCase();
}
Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
if ("png".equals(suffix)) {
format = Bitmap.CompressFormat.PNG;
} else if ("webp".equals(suffix)) {
format = Bitmap.CompressFormat.WEBP;
} else {
suffix = "jpg";
}

File outputDir = getContext().getCacheDir();
File outputFile;
try {
Api.AccessToken accessToken = null;
if (mListener != null) {
accessToken = mListener.getEffectiveAccessToken();
}
String serverUrl = mDiscussion.getPostAttachmentsUrl(getAttachmentHash(), accessToken);
outputFile = File.createTempFile(prefix, "." + suffix, outputDir);
FileOutputStream out = new FileOutputStream(outputFile);
resource.compress(format, 70, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
return;
}

String path = uri.toString();
if (path.startsWith("file:///")) {
path = path.substring(7);
}
uploadAttach(uri, outputFile.getPath(), fileName);
}

void uploadAttach(Uri uri) {
int size = App.getFeatureAttachmentResize();
if (size > 0) {
attemptResize(uri, size);
} else {
uploadAttach(uri, uri.getPath(), null);
}
}

void uploadAttach(Uri uri, String path, String fileName) {
Api.AccessToken accessToken = null;
if (mListener != null) {
accessToken = mListener.getEffectiveAccessToken();
}
if (accessToken == null) {
return;
}

String serverUrl = mDiscussion.getPostAttachmentsUrl(getAttachmentHash(), accessToken);
if (TextUtils.isEmpty(serverUrl)) {
return;
}

if (path.startsWith("file:///")) {
path = path.substring(7);
}

try {
String uploadId = new MultipartUploadRequest(getContext(), serverUrl)
.addFileToUpload(path, Api.PARAM_FILE)
.addFileToUpload(path, Api.PARAM_FILE, fileName)
.setUtf8Charset()
.setAutoDeleteFilesAfterSuccessfulUpload(true)
.setMaxRetries(2)
.setDelegate(new UploadStatusDelegate() {
@Override
Expand Down

0 comments on commit dd25c58

Please sign in to comment.