doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.doumee.lib_coremodel.util.luban;
 
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
@SuppressWarnings("unused")
public class Luban implements Handler.Callback {
  private static final String TAG = "Luban";
  private static final String DEFAULT_DISK_CACHE_DIR = "luban_disk_cache";
 
  private static final int MSG_COMPRESS_SUCCESS = 0;
  private static final int MSG_COMPRESS_START = 1;
  private static final int MSG_COMPRESS_ERROR = 2;
 
  private String mTargetDir;
  private boolean focusAlpha;
  private int mLeastCompressSize;
  private OnRenameListener mRenameListener;
  private OnCompressListener mCompressListener;
  private CompressionPredicate mCompressionPredicate;
  private List<InputStreamProvider> mStreamProviders;
 
  private Handler mHandler;
 
  private Luban(Builder builder) {
    this.mTargetDir = builder.mTargetDir;
    this.mRenameListener = builder.mRenameListener;
    this.mStreamProviders = builder.mStreamProviders;
    this.mCompressListener = builder.mCompressListener;
    this.mLeastCompressSize = builder.mLeastCompressSize;
    this.mCompressionPredicate = builder.mCompressionPredicate;
    mHandler = new Handler(Looper.getMainLooper(), this);
  }
 
  public static Builder with(Context context) {
    return new Builder(context);
  }
 
  /**
   * Returns a file with a cache image name in the private cache directory.
   *
   * @param context A context.
   */
  private File getImageCacheFile(Context context, String suffix) {
    if (TextUtils.isEmpty(mTargetDir)) {
      mTargetDir = getImageCacheDir(context).getAbsolutePath();
    }
 
    String cacheBuilder = mTargetDir + "/" +
        System.currentTimeMillis() +
        (int) (Math.random() * 1000) +
        (TextUtils.isEmpty(suffix) ? ".jpg" : suffix);
 
    return new File(cacheBuilder);
  }
 
  private File getImageCustomFile(Context context, String filename) {
    if (TextUtils.isEmpty(mTargetDir)) {
      mTargetDir = getImageCacheDir(context).getAbsolutePath();
    }
 
    String cacheBuilder = mTargetDir + "/" + filename;
 
    return new File(cacheBuilder);
  }
 
  /**
   * Returns a directory with a default name in the private cache directory of the application to
   * use to store retrieved audio.
   *
   * @param context A context.
   * @see #getImageCacheDir(Context, String)
   */
  private File getImageCacheDir(Context context) {
    return getImageCacheDir(context, DEFAULT_DISK_CACHE_DIR);
  }
 
  /**
   * Returns a directory with the given name in the private cache directory of the application to
   * use to store retrieved media and thumbnails.
   *
   * @param context   A context.
   * @param cacheName The name of the subdirectory in which to store the cache.
   * @see #getImageCacheDir(Context)
   */
  private static File getImageCacheDir(Context context, String cacheName) {
    File cacheDir = context.getExternalCacheDir();
    if (cacheDir != null) {
      File result = new File(cacheDir, cacheName);
      if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) {
        // File wasn't able to create a directory, or the result exists but not a directory
        return null;
      }
      return result;
    }
    if (Log.isLoggable(TAG, Log.ERROR)) {
      Log.e(TAG, "default disk cache dir is null");
    }
    return null;
  }
 
  /**
   * start asynchronous compress thread
   */
  private void launch(final Context context) {
    if (mStreamProviders == null || mStreamProviders.size() == 0 && mCompressListener != null) {
      mCompressListener.onError(new NullPointerException("image file cannot be null"));
    }
 
    Iterator<InputStreamProvider> iterator = mStreamProviders.iterator();
 
    while (iterator.hasNext()) {
      final InputStreamProvider path = iterator.next();
 
      AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
        @Override
        public void run() {
          try {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START));
 
            File result = compress(context, path);
 
            mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_SUCCESS, result));
          } catch (IOException e) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_ERROR, e));
          }
        }
      });
 
      iterator.remove();
    }
  }
 
  /**
   * start compress and return the file
   */
  private File get(InputStreamProvider input, Context context) throws IOException {
    try {
      return new Engine(input, getImageCacheFile(context, Checker.SINGLE.extSuffix(input)), focusAlpha).compress();
    } finally {
      input.close();
    }
  }
 
  private List<File> get(Context context) throws IOException {
    List<File> results = new ArrayList<>();
    Iterator<InputStreamProvider> iterator = mStreamProviders.iterator();
 
    while (iterator.hasNext()) {
      results.add(compress(context, iterator.next()));
      iterator.remove();
    }
 
    return results;
  }
 
  private File compress(Context context, InputStreamProvider path) throws IOException {
    try {
      return compressReal(context,path);
    } finally {
      path.close();
    }
  }
 
  private File compressReal(Context context, InputStreamProvider path) throws IOException {
    File result;
 
    File outFile = getImageCacheFile(context, Checker.SINGLE.extSuffix(path));
 
    if (mRenameListener != null) {
      String filename = mRenameListener.rename(path.getPath());
      outFile = getImageCustomFile(context, filename);
    }
 
    if (mCompressionPredicate != null) {
      if (mCompressionPredicate.apply(path.getPath())
          && Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath())) {
        result = new Engine(path, outFile, focusAlpha).compress();
      } else {
        result = new File(path.getPath());
      }
    } else {
      result = Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath()) ?
          new Engine(path, outFile, focusAlpha).compress() :
          new File(path.getPath());
    }
 
    return result;
  }
 
  @Override
  public boolean handleMessage(Message msg) {
    if (mCompressListener == null) {
      return false;
    }
 
    switch (msg.what) {
      case MSG_COMPRESS_START:
        mCompressListener.onStart();
        break;
      case MSG_COMPRESS_SUCCESS:
        mCompressListener.onSuccess((File) msg.obj);
        break;
      case MSG_COMPRESS_ERROR:
        mCompressListener.onError((Throwable) msg.obj);
        break;
    }
    return false;
  }
 
  public static class Builder {
    private Context context;
    private String mTargetDir;
    private boolean focusAlpha;
    private int mLeastCompressSize = 100;
    private OnRenameListener mRenameListener;
    private OnCompressListener mCompressListener;
    private CompressionPredicate mCompressionPredicate;
    private List<InputStreamProvider> mStreamProviders;
 
    Builder(Context context) {
      this.context = context;
      this.mStreamProviders = new ArrayList<>();
    }
 
    private Luban build() {
      return new Luban(this);
    }
 
    public Builder load(InputStreamProvider inputStreamProvider) {
      mStreamProviders.add(inputStreamProvider);
      return this;
    }
 
    public Builder load(final File file) {
      mStreamProviders.add(new InputStreamAdapter() {
        @Override
        public InputStream openInternal() throws IOException {
          return new FileInputStream(file);
        }
 
        @Override
        public String getPath() {
          return file.getAbsolutePath();
        }
      });
      return this;
    }
 
    public Builder load(final String string) {
      mStreamProviders.add(new InputStreamAdapter() {
        @Override
        public InputStream openInternal() throws IOException {
          return new FileInputStream(string);
        }
 
        @Override
        public String getPath() {
          return string;
        }
      });
      return this;
    }
 
    public <T> Builder load(List<T> list) {
      for (T src : list) {
        if (src instanceof String) {
          load((String) src);
        } else if (src instanceof File) {
          load((File) src);
        } else if (src instanceof Uri) {
          load((Uri) src);
        } else {
          throw new IllegalArgumentException("Incoming data type exception, it must be String, File, Uri or Bitmap");
        }
      }
      return this;
    }
 
    public Builder load(final Uri uri) {
      mStreamProviders.add(new InputStreamAdapter() {
        @Override
        public InputStream openInternal() throws IOException {
          return context.getContentResolver().openInputStream(uri);
        }
 
        @Override
        public String getPath() {
          return uri.getPath();
        }
      });
      return this;
    }
 
    public Builder putGear(int gear) {
      return this;
    }
 
    public Builder setRenameListener(OnRenameListener listener) {
      this.mRenameListener = listener;
      return this;
    }
 
    public Builder setCompressListener(OnCompressListener listener) {
      this.mCompressListener = listener;
      return this;
    }
 
    public Builder setTargetDir(String targetDir) {
      this.mTargetDir = targetDir;
      return this;
    }
 
    /**
     * Do I need to keep the image's alpha channel
     *
     * @param focusAlpha <p> true - to keep alpha channel, the compress speed will be slow. </p>
     *                   <p> false - don't keep alpha channel, it might have a black background.</p>
     */
    public Builder setFocusAlpha(boolean focusAlpha) {
      this.focusAlpha = focusAlpha;
      return this;
    }
 
    /**
     * do not compress when the origin image file size less than one value
     *
     * @param size the value of file size, unit KB, default 100K
     */
    public Builder ignoreBy(int size) {
      this.mLeastCompressSize = size;
      return this;
    }
 
    /**
     * do compress image when return value was true, otherwise, do not compress the image file
     *
     * @param compressionPredicate A predicate callback that returns true or false for the given input path should be compressed.
     */
    public Builder filter(CompressionPredicate compressionPredicate) {
      this.mCompressionPredicate = compressionPredicate;
      return this;
    }
 
 
    /**
     * begin compress image with asynchronous
     */
    public void launch() {
      build().launch(context);
    }
 
    public File get(final String path) throws IOException {
      return build().get(new InputStreamAdapter() {
        @Override
        public InputStream openInternal() throws IOException {
          return new FileInputStream(path);
        }
 
        @Override
        public String getPath() {
          return path;
        }
      }, context);
    }
 
    /**
     * begin compress image with synchronize
     *
     * @return the thumb image file list
     */
    public List<File> get() throws IOException {
      return build().get(context);
    }
  }
}