Commit 0a36934574e8dbe0d67bdd96d7abd0eb15c4baa9
1 parent
cdd48e87
1 当子类包含fragment时,自动回调onActivityResult方法
Showing
12 changed files
with
19 additions
and
789 deletions
app/strike/src/main/java/com/cnlive/strike/MainActivity.java
| ... | ... | @@ -28,7 +28,7 @@ public class MainActivity extends AppCompatActivity { |
| 28 | 28 | findViewById(R.id.go_to).setOnClickListener(new View.OnClickListener() { |
| 29 | 29 | @Override |
| 30 | 30 | public void onClick(View v) { |
| 31 | - startActivity(new Intent(MainActivity.this, MyActivity.class)); | |
| 31 | + startActivity(new Intent(MainActivity.this, LoginActivity.class)); | |
| 32 | 32 | // finish(); |
| 33 | 33 | } |
| 34 | 34 | }); | ... | ... |
cloud/user/src/main/java/com/cnlive/strike/user/frame/presenter/QuickLoginPresenter.java
| ... | ... | @@ -164,9 +164,9 @@ public class QuickLoginPresenter extends BasePresenter<QuickLoginView> { |
| 164 | 164 | } else { |
| 165 | 165 | //友盟账号登录统计,暂时只有平台登录,后续对接三方登录 |
| 166 | 166 | // MobclickAgent.onProfileSignIn("CNLive", user.getUid()); |
| 167 | - ifViewAttached(view -> view.showMainView(false, false)); | |
| 167 | +// ifViewAttached(view -> view.showMainView(false, false)); | |
| 168 | 168 | //todo 测试首次用户 |
| 169 | -// ifViewAttached(view -> view.showEditView(false)); | |
| 169 | + ifViewAttached(view -> view.showEditView(false)); | |
| 170 | 170 | } |
| 171 | 171 | AppConfig.setCountryCode(""); |
| 172 | 172 | AppConfig.setVerificationCode(""); | ... | ... |
cloud/user/src/main/java/com/cnlive/strike/user/ui/activity/FirstLoginActivity.java
| ... | ... | @@ -24,10 +24,6 @@ public class FirstLoginActivity extends BaseActivity { |
| 24 | 24 | private String tagMsg; |
| 25 | 25 | public static String TAG_CLICK_AD = "clickAd"; |
| 26 | 26 | |
| 27 | - @Override | |
| 28 | - protected int getViewLayoutId() { | |
| 29 | - return R.layout.activity_login; | |
| 30 | - } | |
| 31 | 27 | |
| 32 | 28 | @Override |
| 33 | 29 | protected void initParam() { |
| ... | ... | @@ -44,9 +40,9 @@ public class FirstLoginActivity extends BaseActivity { |
| 44 | 40 | @Override |
| 45 | 41 | protected void initView() { |
| 46 | 42 | super.initView(); |
| 47 | - FragmentManager fragmentManager = getSupportFragmentManager(); | |
| 48 | - FragmentUtil fragmentUtil = new FragmentUtil(R.id.loginContent); | |
| 49 | - fragmentUtil.initFragment(firstLoginFragment, fragmentManager); | |
| 43 | + if (null != firstLoginFragment) { | |
| 44 | + loadRootFragment(firstLoginFragment); | |
| 45 | + } | |
| 50 | 46 | } |
| 51 | 47 | |
| 52 | 48 | @Override |
| ... | ... | @@ -54,18 +50,6 @@ public class FirstLoginActivity extends BaseActivity { |
| 54 | 50 | super.onDestroy(); |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | - @Override | |
| 58 | - protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| 59 | - super.onActivityResult(requestCode, resultCode, data); | |
| 60 | - firstLoginFragment.onActivityResult(requestCode, resultCode, data); | |
| 61 | - } | |
| 62 | - | |
| 63 | - @Override | |
| 64 | - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
| 65 | - super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
| 66 | - if (firstLoginFragment != null) | |
| 67 | - firstLoginFragment.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
| 68 | - } | |
| 69 | 53 | |
| 70 | 54 | @Override |
| 71 | 55 | public void onBackPressed() { | ... | ... |
cloud/user/src/main/java/com/cnlive/strike/user/ui/activity/LoginActivity.java
| ... | ... | @@ -91,13 +91,6 @@ public class LoginActivity extends BaseActivity { |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | @Override |
| 94 | - public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| 95 | - super.onActivityResult(requestCode, resultCode, data); | |
| 96 | - if (quickLoginFragment != null) | |
| 97 | - quickLoginFragment.onActivityResult(requestCode, resultCode, data); | |
| 98 | - } | |
| 99 | - | |
| 100 | - @Override | |
| 101 | 94 | protected void onNewIntent(Intent intent) { |
| 102 | 95 | super.onNewIntent(intent); |
| 103 | 96 | tagMsg = intent.getStringExtra(TAG_MSG) == null ? "" : intent.getStringExtra(TAG_MSG); | ... | ... |
core/base/src/main/java/com/cnlive/core/libs/base/frame/activity/BaseActivity.java
| ... | ... | @@ -32,6 +32,7 @@ import androidx.core.app.ActivityCompat; |
| 32 | 32 | import androidx.core.content.ContextCompat; |
| 33 | 33 | import androidx.databinding.DataBindingUtil; |
| 34 | 34 | import androidx.databinding.ViewDataBinding; |
| 35 | +import androidx.fragment.app.Fragment; | |
| 35 | 36 | |
| 36 | 37 | import com.alibaba.android.arouter.launcher.ARouter; |
| 37 | 38 | import com.cnlive.core.libs.base.application.AppConfig; |
| ... | ... | @@ -1015,4 +1016,16 @@ public abstract class BaseActivity<V extends BaseView, P extends BasePresenter<V |
| 1015 | 1016 | return mSwipeDelegate.getSwipeBackLayout(); |
| 1016 | 1017 | } |
| 1017 | 1018 | |
| 1019 | + | |
| 1020 | + @Override | |
| 1021 | + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |
| 1022 | + super.onActivityResult(requestCode, resultCode, data); | |
| 1023 | + if (null!=getSupportFragmentManager()&&null != getSupportFragmentManager().getFragments()) { | |
| 1024 | + for (Fragment fragment : getSupportFragmentManager().getFragments()) { | |
| 1025 | + if (null != fragment) { | |
| 1026 | + fragment.onActivityResult(requestCode, resultCode, data); | |
| 1027 | + } | |
| 1028 | + } | |
| 1029 | + } | |
| 1030 | + } | |
| 1018 | 1031 | } |
| 1019 | 1032 | \ No newline at end of file | ... | ... |
core/base/src/main/java/top/zibin/luban/Checker.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -import android.graphics.BitmapFactory; | |
| 4 | -import android.util.Log; | |
| 5 | - | |
| 6 | -import java.io.ByteArrayOutputStream; | |
| 7 | -import java.io.File; | |
| 8 | -import java.io.IOException; | |
| 9 | -import java.io.InputStream; | |
| 10 | -import java.util.ArrayList; | |
| 11 | -import java.util.Arrays; | |
| 12 | -import java.util.List; | |
| 13 | - | |
| 14 | -enum Checker { | |
| 15 | - SINGLE; | |
| 16 | - | |
| 17 | - private static final String TAG = "Luban"; | |
| 18 | - | |
| 19 | - private static List<String> format = new ArrayList<>(); | |
| 20 | - private static final String JPG = ".jpg"; | |
| 21 | - private static final String JPEG = ".jpeg"; | |
| 22 | - private static final String PNG = ".png"; | |
| 23 | - private static final String WEBP = ".webp"; | |
| 24 | - private static final String GIF = ".gif"; | |
| 25 | - | |
| 26 | - private final byte[] JPEG_SIGNATURE = new byte[]{(byte) 0xFF, (byte) 0xD8, (byte) 0xFF}; | |
| 27 | - | |
| 28 | - static { | |
| 29 | - format.add(JPG); | |
| 30 | - format.add(JPEG); | |
| 31 | - format.add(PNG); | |
| 32 | - format.add(WEBP); | |
| 33 | - format.add(GIF); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Determine if it is JPG. | |
| 38 | - * | |
| 39 | - * @param is image file input stream | |
| 40 | - */ | |
| 41 | - boolean isJPG(InputStream is) { | |
| 42 | - return isJPG(toByteArray(is)); | |
| 43 | - } | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Returns the degrees in clockwise. Values are 0, 90, 180, or 270. | |
| 47 | - */ | |
| 48 | - int getOrientation(InputStream is) { | |
| 49 | - return getOrientation(toByteArray(is)); | |
| 50 | - } | |
| 51 | - | |
| 52 | - private boolean isJPG(byte[] data) { | |
| 53 | - if (data == null || data.length < 3) { | |
| 54 | - return false; | |
| 55 | - } | |
| 56 | - byte[] signatureB = new byte[]{data[0], data[1], data[2]}; | |
| 57 | - return Arrays.equals(JPEG_SIGNATURE, signatureB); | |
| 58 | - } | |
| 59 | - | |
| 60 | - private int getOrientation(byte[] jpeg) { | |
| 61 | - if (jpeg == null) { | |
| 62 | - return 0; | |
| 63 | - } | |
| 64 | - | |
| 65 | - int offset = 0; | |
| 66 | - int length = 0; | |
| 67 | - | |
| 68 | - // ISO/IEC 10918-1:1993(E) | |
| 69 | - while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) { | |
| 70 | - int marker = jpeg[offset] & 0xFF; | |
| 71 | - | |
| 72 | - // Check if the marker is a padding. | |
| 73 | - if (marker == 0xFF) { | |
| 74 | - continue; | |
| 75 | - } | |
| 76 | - offset++; | |
| 77 | - | |
| 78 | - // Check if the marker is SOI or TEM. | |
| 79 | - if (marker == 0xD8 || marker == 0x01) { | |
| 80 | - continue; | |
| 81 | - } | |
| 82 | - // Check if the marker is EOI or SOS. | |
| 83 | - if (marker == 0xD9 || marker == 0xDA) { | |
| 84 | - break; | |
| 85 | - } | |
| 86 | - | |
| 87 | - // Get the length and check if it is reasonable. | |
| 88 | - length = pack(jpeg, offset, 2, false); | |
| 89 | - if (length < 2 || offset + length > jpeg.length) { | |
| 90 | - Log.e(TAG, "Invalid length"); | |
| 91 | - return 0; | |
| 92 | - } | |
| 93 | - | |
| 94 | - // Break if the marker is EXIF in APP1. | |
| 95 | - if (marker == 0xE1 && length >= 8 | |
| 96 | - && pack(jpeg, offset + 2, 4, false) == 0x45786966 | |
| 97 | - && pack(jpeg, offset + 6, 2, false) == 0) { | |
| 98 | - offset += 8; | |
| 99 | - length -= 8; | |
| 100 | - break; | |
| 101 | - } | |
| 102 | - | |
| 103 | - // Skip other markers. | |
| 104 | - offset += length; | |
| 105 | - length = 0; | |
| 106 | - } | |
| 107 | - | |
| 108 | - // JEITA CP-3451 Exif Version 2.2 | |
| 109 | - if (length > 8) { | |
| 110 | - // Identify the byte order. | |
| 111 | - int tag = pack(jpeg, offset, 4, false); | |
| 112 | - if (tag != 0x49492A00 && tag != 0x4D4D002A) { | |
| 113 | - Log.e(TAG, "Invalid byte order"); | |
| 114 | - return 0; | |
| 115 | - } | |
| 116 | - boolean littleEndian = (tag == 0x49492A00); | |
| 117 | - | |
| 118 | - // Get the offset and check if it is reasonable. | |
| 119 | - int count = pack(jpeg, offset + 4, 4, littleEndian) + 2; | |
| 120 | - if (count < 10 || count > length) { | |
| 121 | - Log.e(TAG, "Invalid offset"); | |
| 122 | - return 0; | |
| 123 | - } | |
| 124 | - offset += count; | |
| 125 | - length -= count; | |
| 126 | - | |
| 127 | - // Get the count and go through all the elements. | |
| 128 | - count = pack(jpeg, offset - 2, 2, littleEndian); | |
| 129 | - while (count-- > 0 && length >= 12) { | |
| 130 | - // Get the tag and check if it is orientation. | |
| 131 | - tag = pack(jpeg, offset, 2, littleEndian); | |
| 132 | - if (tag == 0x0112) { | |
| 133 | - int orientation = pack(jpeg, offset + 8, 2, littleEndian); | |
| 134 | - switch (orientation) { | |
| 135 | - case 1: | |
| 136 | - return 0; | |
| 137 | - case 3: | |
| 138 | - return 180; | |
| 139 | - case 6: | |
| 140 | - return 90; | |
| 141 | - case 8: | |
| 142 | - return 270; | |
| 143 | - } | |
| 144 | - Log.e(TAG, "Unsupported orientation"); | |
| 145 | - return 0; | |
| 146 | - } | |
| 147 | - offset += 12; | |
| 148 | - length -= 12; | |
| 149 | - } | |
| 150 | - } | |
| 151 | - | |
| 152 | - Log.e(TAG, "Orientation not found"); | |
| 153 | - return 0; | |
| 154 | - } | |
| 155 | - | |
| 156 | - String extSuffix(InputStreamProvider input) { | |
| 157 | - try { | |
| 158 | - BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 159 | - options.inJustDecodeBounds = true; | |
| 160 | - BitmapFactory.decodeStream(input.open(), null, options); | |
| 161 | - return options.outMimeType.replace("image/", "."); | |
| 162 | - } catch (Exception e) { | |
| 163 | - return JPG; | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - boolean needCompress(int leastCompressSize, String path) { | |
| 168 | - if (leastCompressSize > 0) { | |
| 169 | - File source = new File(path); | |
| 170 | - return source.exists() && source.length() > (leastCompressSize << 10); | |
| 171 | - } | |
| 172 | - return true; | |
| 173 | - } | |
| 174 | - | |
| 175 | - private int pack(byte[] bytes, int offset, int length, boolean littleEndian) { | |
| 176 | - int step = 1; | |
| 177 | - if (littleEndian) { | |
| 178 | - offset += length - 1; | |
| 179 | - step = -1; | |
| 180 | - } | |
| 181 | - | |
| 182 | - int value = 0; | |
| 183 | - while (length-- > 0) { | |
| 184 | - value = (value << 8) | (bytes[offset] & 0xFF); | |
| 185 | - offset += step; | |
| 186 | - } | |
| 187 | - return value; | |
| 188 | - } | |
| 189 | - | |
| 190 | - private byte[] toByteArray(InputStream is) { | |
| 191 | - if (is == null) { | |
| 192 | - return new byte[0]; | |
| 193 | - } | |
| 194 | - | |
| 195 | - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | |
| 196 | - | |
| 197 | - int read; | |
| 198 | - byte[] data = new byte[4096]; | |
| 199 | - | |
| 200 | - try { | |
| 201 | - while ((read = is.read(data, 0, data.length)) != -1) { | |
| 202 | - buffer.write(data, 0, read); | |
| 203 | - } | |
| 204 | - } catch (Exception ignored) { | |
| 205 | - return new byte[0]; | |
| 206 | - } finally { | |
| 207 | - try { | |
| 208 | - buffer.close(); | |
| 209 | - } catch (IOException ignored) { | |
| 210 | - } | |
| 211 | - } | |
| 212 | - | |
| 213 | - return buffer.toByteArray(); | |
| 214 | - } | |
| 215 | -} |
core/base/src/main/java/top/zibin/luban/CompressionPredicate.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Created on 2018/1/3 19:43 | |
| 5 | - * | |
| 6 | - * @author andy | |
| 7 | - * | |
| 8 | - * A functional interface (callback) that returns true or false for the given input path should be compressed. | |
| 9 | - */ | |
| 10 | - | |
| 11 | -public interface CompressionPredicate { | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * Determine the given input path should be compressed and return a boolean. | |
| 15 | - * @param path input path | |
| 16 | - * @return the boolean result | |
| 17 | - */ | |
| 18 | - boolean apply(String path); | |
| 19 | -} |
core/base/src/main/java/top/zibin/luban/Engine.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -import android.graphics.Bitmap; | |
| 4 | -import android.graphics.BitmapFactory; | |
| 5 | -import android.graphics.Matrix; | |
| 6 | - | |
| 7 | -import java.io.ByteArrayOutputStream; | |
| 8 | -import java.io.File; | |
| 9 | -import java.io.FileOutputStream; | |
| 10 | -import java.io.IOException; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * Responsible for starting compress and managing active and cached resources. | |
| 14 | - */ | |
| 15 | -class Engine { | |
| 16 | - private InputStreamProvider srcImg; | |
| 17 | - private File tagImg; | |
| 18 | - private int srcWidth; | |
| 19 | - private int srcHeight; | |
| 20 | - private boolean focusAlpha; | |
| 21 | - | |
| 22 | - Engine(InputStreamProvider srcImg, File tagImg, boolean focusAlpha) throws IOException { | |
| 23 | - this.tagImg = tagImg; | |
| 24 | - this.srcImg = srcImg; | |
| 25 | - this.focusAlpha = focusAlpha; | |
| 26 | - | |
| 27 | - BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 28 | - options.inJustDecodeBounds = true; | |
| 29 | - options.inSampleSize = 1; | |
| 30 | - | |
| 31 | - BitmapFactory.decodeStream(srcImg.open(), null, options); | |
| 32 | - this.srcWidth = options.outWidth; | |
| 33 | - this.srcHeight = options.outHeight; | |
| 34 | - } | |
| 35 | - | |
| 36 | - private int computeSize() { | |
| 37 | - srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth; | |
| 38 | - srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight; | |
| 39 | - | |
| 40 | - int longSide = Math.max(srcWidth, srcHeight); | |
| 41 | - int shortSide = Math.min(srcWidth, srcHeight); | |
| 42 | - | |
| 43 | - float scale = ((float) shortSide / longSide); | |
| 44 | - if (scale <= 1 && scale > 0.5625) { | |
| 45 | - if (longSide < 1664) { | |
| 46 | - return 1; | |
| 47 | - } else if (longSide < 4990) { | |
| 48 | - return 2; | |
| 49 | - } else if (longSide > 4990 && longSide < 10240) { | |
| 50 | - return 4; | |
| 51 | - } else { | |
| 52 | - return longSide / 1280 == 0 ? 1 : longSide / 1280; | |
| 53 | - } | |
| 54 | - } else if (scale <= 0.5625 && scale > 0.5) { | |
| 55 | - return longSide / 1280 == 0 ? 1 : longSide / 1280; | |
| 56 | - } else { | |
| 57 | - //TODO 增加最大像素限制 | |
| 58 | - return Math.max((int) Math.ceil(longSide / (1280.0 / scale)), 1 + (longSide / 20480)); | |
| 59 | - } | |
| 60 | - } | |
| 61 | - | |
| 62 | - private Bitmap rotatingImage(Bitmap bitmap, int angle) { | |
| 63 | - Matrix matrix = new Matrix(); | |
| 64 | - | |
| 65 | - matrix.postRotate(angle); | |
| 66 | - | |
| 67 | - return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); | |
| 68 | - } | |
| 69 | - | |
| 70 | - File compress() throws IOException { | |
| 71 | - BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 72 | - options.inSampleSize = computeSize(); | |
| 73 | - | |
| 74 | - Bitmap tagBitmap = BitmapFactory.decodeStream(srcImg.open(), null, options); | |
| 75 | - ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
| 76 | - | |
| 77 | - if (Checker.SINGLE.isJPG(srcImg.open())) { | |
| 78 | - tagBitmap = rotatingImage(tagBitmap, Checker.SINGLE.getOrientation(srcImg.open())); | |
| 79 | - } | |
| 80 | - tagBitmap.compress(focusAlpha ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 60, stream); | |
| 81 | - tagBitmap.recycle(); | |
| 82 | - | |
| 83 | - FileOutputStream fos = new FileOutputStream(tagImg); | |
| 84 | - fos.write(stream.toByteArray()); | |
| 85 | - fos.flush(); | |
| 86 | - fos.close(); | |
| 87 | - stream.close(); | |
| 88 | - | |
| 89 | - return tagImg; | |
| 90 | - } | |
| 91 | -} | |
| 92 | 0 | \ No newline at end of file |
core/base/src/main/java/top/zibin/luban/InputStreamProvider.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.InputStream; | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * 通过此接口获取输入流,以兼容文件、FileProvider方式获取到的图片 | |
| 8 | - * <presenter> | |
| 9 | - * Get the input stream through this interface, and obtain the picture using compatible files and FileProvider | |
| 10 | - */ | |
| 11 | -public interface InputStreamProvider { | |
| 12 | - | |
| 13 | - InputStream open() throws IOException; | |
| 14 | - | |
| 15 | - String getPath(); | |
| 16 | -} |
core/base/src/main/java/top/zibin/luban/Luban.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -import android.content.Context; | |
| 4 | -import android.net.Uri; | |
| 5 | -import android.os.AsyncTask; | |
| 6 | -import android.os.Handler; | |
| 7 | -import android.os.Looper; | |
| 8 | -import android.os.Message; | |
| 9 | -import android.text.TextUtils; | |
| 10 | -import android.util.Log; | |
| 11 | - | |
| 12 | -import java.io.File; | |
| 13 | -import java.io.FileInputStream; | |
| 14 | -import java.io.IOException; | |
| 15 | -import java.io.InputStream; | |
| 16 | -import java.util.ArrayList; | |
| 17 | -import java.util.Iterator; | |
| 18 | -import java.util.List; | |
| 19 | - | |
| 20 | -@SuppressWarnings("unused") | |
| 21 | -public class Luban implements Handler.Callback { | |
| 22 | - private static final String TAG = "Luban"; | |
| 23 | - private static final String DEFAULT_DISK_CACHE_DIR = "luban_disk_cache"; | |
| 24 | - | |
| 25 | - private static final int MSG_COMPRESS_SUCCESS = 0; | |
| 26 | - private static final int MSG_COMPRESS_START = 1; | |
| 27 | - private static final int MSG_COMPRESS_ERROR = 2; | |
| 28 | - | |
| 29 | - private String mTargetDir; | |
| 30 | - private boolean focusAlpha; | |
| 31 | - private int mLeastCompressSize; | |
| 32 | - private OnRenameListener mRenameListener; | |
| 33 | - private OnCompressListener mCompressListener; | |
| 34 | - private CompressionPredicate mCompressionPredicate; | |
| 35 | - private List<InputStreamProvider> mStreamProviders; | |
| 36 | - | |
| 37 | - private Handler mHandler; | |
| 38 | - | |
| 39 | - private Luban(Builder builder) { | |
| 40 | - this.mTargetDir = builder.mTargetDir; | |
| 41 | - this.mRenameListener = builder.mRenameListener; | |
| 42 | - this.mStreamProviders = builder.mStreamProviders; | |
| 43 | - this.mCompressListener = builder.mCompressListener; | |
| 44 | - this.mLeastCompressSize = builder.mLeastCompressSize; | |
| 45 | - this.mCompressionPredicate = builder.mCompressionPredicate; | |
| 46 | - mHandler = new Handler(Looper.getMainLooper(), this); | |
| 47 | - } | |
| 48 | - | |
| 49 | - public static Builder with(Context context) { | |
| 50 | - return new Builder(context); | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Returns a file with a cache image name in the private cache directory. | |
| 55 | - * | |
| 56 | - * @param context A context. | |
| 57 | - */ | |
| 58 | - private File getImageCacheFile(Context context, String suffix) { | |
| 59 | - if (TextUtils.isEmpty(mTargetDir)) { | |
| 60 | - mTargetDir = getImageCacheDir(context).getAbsolutePath(); | |
| 61 | - } | |
| 62 | - | |
| 63 | - String cacheBuilder = mTargetDir + "/" + | |
| 64 | - System.currentTimeMillis() + | |
| 65 | - (int) (Math.random() * 1000) + | |
| 66 | - (TextUtils.isEmpty(suffix) ? ".jpg" : suffix); | |
| 67 | - | |
| 68 | - return new File(cacheBuilder); | |
| 69 | - } | |
| 70 | - | |
| 71 | - private File getImageCustomFile(Context context, String filename) { | |
| 72 | - if (TextUtils.isEmpty(mTargetDir)) { | |
| 73 | - mTargetDir = getImageCacheDir(context).getAbsolutePath(); | |
| 74 | - } | |
| 75 | - | |
| 76 | - String cacheBuilder = mTargetDir + "/" + filename; | |
| 77 | - | |
| 78 | - return new File(cacheBuilder); | |
| 79 | - } | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * Returns a directory with a default name in the private cache directory of the application to | |
| 83 | - * use to store retrieved audio. | |
| 84 | - * | |
| 85 | - * @param context A context. | |
| 86 | - * @see #getImageCacheDir(Context, String) | |
| 87 | - */ | |
| 88 | - private File getImageCacheDir(Context context) { | |
| 89 | - return getImageCacheDir(context, DEFAULT_DISK_CACHE_DIR); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Returns a directory with the given name in the private cache directory of the application to | |
| 94 | - * use to store retrieved media and thumbnails. | |
| 95 | - * | |
| 96 | - * @param context A context. | |
| 97 | - * @param cacheName The name of the subdirectory in which to store the cache. | |
| 98 | - * @see #getImageCacheDir(Context) | |
| 99 | - */ | |
| 100 | - private static File getImageCacheDir(Context context, String cacheName) { | |
| 101 | - File cacheDir = context.getExternalCacheDir(); | |
| 102 | - if (cacheDir != null) { | |
| 103 | - File result = new File(cacheDir, cacheName); | |
| 104 | - if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) { | |
| 105 | - // File wasn't able to create a directory, or the result exists but not a directory | |
| 106 | - return null; | |
| 107 | - } | |
| 108 | - return result; | |
| 109 | - } | |
| 110 | - if (Log.isLoggable(TAG, Log.ERROR)) { | |
| 111 | - Log.e(TAG, "default disk cache dir is null"); | |
| 112 | - } | |
| 113 | - return null; | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * start asynchronous compress thread | |
| 118 | - */ | |
| 119 | - private void launch(final Context context) { | |
| 120 | - if (mStreamProviders == null || mStreamProviders.size() == 0 && mCompressListener != null) { | |
| 121 | - mCompressListener.onError(new NullPointerException("image file cannot be null")); | |
| 122 | - } | |
| 123 | - | |
| 124 | - Iterator<InputStreamProvider> iterator = mStreamProviders.iterator(); | |
| 125 | - | |
| 126 | - while (iterator.hasNext()) { | |
| 127 | - final InputStreamProvider path = iterator.next(); | |
| 128 | - | |
| 129 | - AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() { | |
| 130 | - @Override | |
| 131 | - public void run() { | |
| 132 | - try { | |
| 133 | - mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START)); | |
| 134 | - | |
| 135 | - File result = compress(context, path); | |
| 136 | - | |
| 137 | - mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_SUCCESS, result)); | |
| 138 | - } catch (IOException e) { | |
| 139 | - mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_ERROR, e)); | |
| 140 | - } | |
| 141 | - } | |
| 142 | - }); | |
| 143 | - | |
| 144 | - iterator.remove(); | |
| 145 | - } | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | - * start compress and return the file | |
| 150 | - */ | |
| 151 | - private File get(InputStreamProvider input, Context context) throws IOException { | |
| 152 | - return new Engine(input, getImageCacheFile(context, Checker.SINGLE.extSuffix(input)), focusAlpha).compress(); | |
| 153 | - } | |
| 154 | - | |
| 155 | - private List<File> get(Context context) throws IOException { | |
| 156 | - List<File> results = new ArrayList<>(); | |
| 157 | - Iterator<InputStreamProvider> iterator = mStreamProviders.iterator(); | |
| 158 | - | |
| 159 | - while (iterator.hasNext()) { | |
| 160 | - results.add(compress(context, iterator.next())); | |
| 161 | - iterator.remove(); | |
| 162 | - } | |
| 163 | - | |
| 164 | - return results; | |
| 165 | - } | |
| 166 | - | |
| 167 | - private File compress(Context context, InputStreamProvider path) throws IOException { | |
| 168 | - File result; | |
| 169 | - | |
| 170 | - File outFile = getImageCacheFile(context, Checker.SINGLE.extSuffix(path)); | |
| 171 | - | |
| 172 | - if (mRenameListener != null) { | |
| 173 | - String filename = mRenameListener.rename(path.getPath()); | |
| 174 | - outFile = getImageCustomFile(context, filename); | |
| 175 | - } | |
| 176 | - | |
| 177 | - if (mCompressionPredicate != null) { | |
| 178 | - if (mCompressionPredicate.apply(path.getPath()) | |
| 179 | - && Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath())) { | |
| 180 | - result = new Engine(path, outFile, focusAlpha).compress(); | |
| 181 | - } else { | |
| 182 | - result = new File(path.getPath()); | |
| 183 | - } | |
| 184 | - } else { | |
| 185 | - result = Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath()) ? | |
| 186 | - new Engine(path, outFile, focusAlpha).compress() : | |
| 187 | - new File(path.getPath()); | |
| 188 | - } | |
| 189 | - | |
| 190 | - return result; | |
| 191 | - } | |
| 192 | - | |
| 193 | - @Override | |
| 194 | - public boolean handleMessage(Message msg) { | |
| 195 | - if (mCompressListener == null) return false; | |
| 196 | - | |
| 197 | - switch (msg.what) { | |
| 198 | - case MSG_COMPRESS_START: | |
| 199 | - mCompressListener.onStart(); | |
| 200 | - break; | |
| 201 | - case MSG_COMPRESS_SUCCESS: | |
| 202 | - mCompressListener.onSuccess((File) msg.obj); | |
| 203 | - break; | |
| 204 | - case MSG_COMPRESS_ERROR: | |
| 205 | - mCompressListener.onError((Throwable) msg.obj); | |
| 206 | - break; | |
| 207 | - } | |
| 208 | - return false; | |
| 209 | - } | |
| 210 | - | |
| 211 | - public static class Builder { | |
| 212 | - private Context context; | |
| 213 | - private String mTargetDir; | |
| 214 | - private boolean focusAlpha; | |
| 215 | - private int mLeastCompressSize = 100; | |
| 216 | - private OnRenameListener mRenameListener; | |
| 217 | - private OnCompressListener mCompressListener; | |
| 218 | - private CompressionPredicate mCompressionPredicate; | |
| 219 | - private List<InputStreamProvider> mStreamProviders; | |
| 220 | - | |
| 221 | - Builder(Context context) { | |
| 222 | - this.context = context; | |
| 223 | - this.mStreamProviders = new ArrayList<>(); | |
| 224 | - } | |
| 225 | - | |
| 226 | - private Luban build() { | |
| 227 | - return new Luban(this); | |
| 228 | - } | |
| 229 | - | |
| 230 | - public Builder load(InputStreamProvider inputStreamProvider) { | |
| 231 | - mStreamProviders.add(inputStreamProvider); | |
| 232 | - return this; | |
| 233 | - } | |
| 234 | - | |
| 235 | - public Builder load(final File file) { | |
| 236 | - mStreamProviders.add(new InputStreamProvider() { | |
| 237 | - @Override | |
| 238 | - public InputStream open() throws IOException { | |
| 239 | - return new FileInputStream(file); | |
| 240 | - } | |
| 241 | - | |
| 242 | - @Override | |
| 243 | - public String getPath() { | |
| 244 | - return file.getAbsolutePath(); | |
| 245 | - } | |
| 246 | - }); | |
| 247 | - return this; | |
| 248 | - } | |
| 249 | - | |
| 250 | - public Builder load(final String string) { | |
| 251 | - mStreamProviders.add(new InputStreamProvider() { | |
| 252 | - @Override | |
| 253 | - public InputStream open() throws IOException { | |
| 254 | - return new FileInputStream(string); | |
| 255 | - } | |
| 256 | - | |
| 257 | - @Override | |
| 258 | - public String getPath() { | |
| 259 | - return string; | |
| 260 | - } | |
| 261 | - }); | |
| 262 | - return this; | |
| 263 | - } | |
| 264 | - | |
| 265 | - public <T> Builder load(List<T> list) { | |
| 266 | - for (T src : list) { | |
| 267 | - if (src instanceof String) { | |
| 268 | - load((String) src); | |
| 269 | - } else if (src instanceof File) { | |
| 270 | - load((File) src); | |
| 271 | - } else if (src instanceof Uri) { | |
| 272 | - load((Uri) src); | |
| 273 | - } else { | |
| 274 | - throw new IllegalArgumentException("Incoming data type exception, it must be String, File, Uri or Bitmap"); | |
| 275 | - } | |
| 276 | - } | |
| 277 | - return this; | |
| 278 | - } | |
| 279 | - | |
| 280 | - public Builder load(final Uri uri) { | |
| 281 | - mStreamProviders.add(new InputStreamProvider() { | |
| 282 | - @Override | |
| 283 | - public InputStream open() throws IOException { | |
| 284 | - return context.getContentResolver().openInputStream(uri); | |
| 285 | - } | |
| 286 | - | |
| 287 | - @Override | |
| 288 | - public String getPath() { | |
| 289 | - return uri.getPath(); | |
| 290 | - } | |
| 291 | - }); | |
| 292 | - return this; | |
| 293 | - } | |
| 294 | - | |
| 295 | - public Builder putGear(int gear) { | |
| 296 | - return this; | |
| 297 | - } | |
| 298 | - | |
| 299 | - public Builder setRenameListener(OnRenameListener listener) { | |
| 300 | - this.mRenameListener = listener; | |
| 301 | - return this; | |
| 302 | - } | |
| 303 | - | |
| 304 | - public Builder setCompressListener(OnCompressListener listener) { | |
| 305 | - this.mCompressListener = listener; | |
| 306 | - return this; | |
| 307 | - } | |
| 308 | - | |
| 309 | - public Builder setTargetDir(String targetDir) { | |
| 310 | - this.mTargetDir = targetDir; | |
| 311 | - return this; | |
| 312 | - } | |
| 313 | - | |
| 314 | - /** | |
| 315 | - * Do I need to keep the image's alpha channel | |
| 316 | - * | |
| 317 | - * @param focusAlpha <presenter> true - to keep alpha channel, the compress speed will be slow. </presenter> | |
| 318 | - * <presenter> false - don't keep alpha channel, it might have a black background.</presenter> | |
| 319 | - */ | |
| 320 | - public Builder setFocusAlpha(boolean focusAlpha) { | |
| 321 | - this.focusAlpha = focusAlpha; | |
| 322 | - return this; | |
| 323 | - } | |
| 324 | - | |
| 325 | - /** | |
| 326 | - * do not compress when the origin image file size less than one value | |
| 327 | - * | |
| 328 | - * @param size the value of file size, unit KB, default 100K | |
| 329 | - */ | |
| 330 | - public Builder ignoreBy(int size) { | |
| 331 | - this.mLeastCompressSize = size; | |
| 332 | - return this; | |
| 333 | - } | |
| 334 | - | |
| 335 | - /** | |
| 336 | - * do compress image when return value was true, otherwise, do not compress the image file | |
| 337 | - * | |
| 338 | - * @param compressionPredicate A predicate callback that returns true or false for the given input path should be compressed. | |
| 339 | - */ | |
| 340 | - public Builder filter(CompressionPredicate compressionPredicate) { | |
| 341 | - this.mCompressionPredicate = compressionPredicate; | |
| 342 | - return this; | |
| 343 | - } | |
| 344 | - | |
| 345 | - | |
| 346 | - /** | |
| 347 | - * begin compress image with asynchronous | |
| 348 | - */ | |
| 349 | - public void launch() { | |
| 350 | - build().launch(context); | |
| 351 | - } | |
| 352 | - | |
| 353 | - public File get(final String path) throws IOException { | |
| 354 | - return build().get(new InputStreamProvider() { | |
| 355 | - @Override | |
| 356 | - public InputStream open() throws IOException { | |
| 357 | - return new FileInputStream(path); | |
| 358 | - } | |
| 359 | - | |
| 360 | - @Override | |
| 361 | - public String getPath() { | |
| 362 | - return path; | |
| 363 | - } | |
| 364 | - }, context); | |
| 365 | - } | |
| 366 | - | |
| 367 | - /** | |
| 368 | - * begin compress image with synchronize | |
| 369 | - * | |
| 370 | - * @return the thumb image file list | |
| 371 | - */ | |
| 372 | - public List<File> get() throws IOException { | |
| 373 | - return build().get(context); | |
| 374 | - } | |
| 375 | - } | |
| 376 | -} | |
| 377 | 0 | \ No newline at end of file |
core/base/src/main/java/top/zibin/luban/OnCompressListener.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -import java.io.File; | |
| 4 | - | |
| 5 | -public interface OnCompressListener { | |
| 6 | - | |
| 7 | - /** | |
| 8 | - * Fired when the compression is started, override to handle in your own code | |
| 9 | - */ | |
| 10 | - void onStart(); | |
| 11 | - | |
| 12 | - /** | |
| 13 | - * Fired when a compression returns successfully, override to handle in your own code | |
| 14 | - */ | |
| 15 | - void onSuccess(File file); | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Fired when a compression fails to complete, override to handle in your own code | |
| 19 | - */ | |
| 20 | - void onError(Throwable e); | |
| 21 | -} |
core/base/src/main/java/top/zibin/luban/OnRenameListener.java deleted
100644 → 0
| 1 | -package top.zibin.luban; | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Author: zibin | |
| 5 | - * Datetime: 2018/5/18 | |
| 6 | - * <presenter> | |
| 7 | - * 提供修改压缩图片命名接口 | |
| 8 | - * <presenter> | |
| 9 | - * A functional interface (callback) that used to rename the file after compress. | |
| 10 | - */ | |
| 11 | -public interface OnRenameListener { | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * 压缩前调用该方法用于修改压缩后文件名 | |
| 15 | - * <presenter> | |
| 16 | - * Call before compression begins. | |
| 17 | - * | |
| 18 | - * @param filePath 传入文件路径/ file path | |
| 19 | - * @return 返回重命名后的字符串/ file name | |
| 20 | - */ | |
| 21 | - String rename(String filePath); | |
| 22 | -} |