commit 09faa49b58669b7fa49016cf5647773ac1eb9c6e Author: GKF Date: Wed May 22 12:19:27 2019 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..2b75303ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 000000000..1bec35e57 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 000000000..79ee123c2 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/dictionaries/GKF.xml b/.idea/dictionaries/GKF.xml new file mode 100644 index 000000000..76ff82ade --- /dev/null +++ b/.idea/dictionaries/GKF.xml @@ -0,0 +1,7 @@ + + + + legado + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 000000000..ada92a5ef --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 000000000..7ac24c777 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..76bef8730 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 000000000..7f68460d8 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 000000000..f18c6026c --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,36 @@ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "io.legado.book" + minSdkVersion 21 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.core:core-ktx:1.0.2' + implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 000000000..f1b424510 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/io/legado/book/ExampleInstrumentedTest.kt b/app/src/androidTest/java/io/legado/book/ExampleInstrumentedTest.kt new file mode 100644 index 000000000..03d6d0207 --- /dev/null +++ b/app/src/androidTest/java/io/legado/book/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package io.legado.book + +import androidx.test.InstrumentationRegistry +import androidx.test.runner.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + assertEquals("cn.legado.book", appContext.packageName) + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 000000000..9416c8f93 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/io/legado/book/App.kt b/app/src/main/java/io/legado/book/App.kt new file mode 100644 index 000000000..fdcf2bfc4 --- /dev/null +++ b/app/src/main/java/io/legado/book/App.kt @@ -0,0 +1,11 @@ +package io.legado.book + +import android.app.Application + +class App : Application() { + + + override fun onCreate() { + super.onCreate() + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/book/utils/ACache.java b/app/src/main/java/io/legado/book/utils/ACache.java new file mode 100644 index 000000000..4489056f5 --- /dev/null +++ b/app/src/main/java/io/legado/book/utils/ACache.java @@ -0,0 +1,818 @@ +//Copyright (c) 2017. 章钦豪. All rights reserved. +package io.legado.book.utils; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.PixelFormat; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import org.json.JSONArray; +import org.json.JSONObject; +import timber.log.Timber; + +import java.io.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +/** + * 本地缓存 + */ +@SuppressWarnings({"unused", "ResultOfMethodCallIgnored", "WeakerAccess"}) +public class ACache { + public static final int TIME_HOUR = 60 * 60; + public static final int TIME_DAY = TIME_HOUR * 24; + private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb + private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量 + private static Map mInstanceMap = new HashMap<>(); + private ACacheManager mCache; + + private ACache(File cacheDir, long max_size, int max_count) { + try { + if (!cacheDir.exists() && !cacheDir.mkdirs()) { + Timber.tag("ACache").i("can't make dirs in %s", cacheDir.getAbsolutePath()); + } + mCache = new ACacheManager(cacheDir, max_size, max_count); + } catch (Exception ignored) { + } + } + + public static ACache get(Context ctx) { + return get(ctx, "ACache"); + } + + public static ACache get(Context ctx, String cacheName) { + File f = new File(ctx.getCacheDir(), cacheName); + return get(f, MAX_SIZE, MAX_COUNT); + } + + public static ACache get(File cacheDir) { + return get(cacheDir, MAX_SIZE, MAX_COUNT); + } + + public static ACache get(Context ctx, long max_zise, int max_count) { + try { + File f = new File(ctx.getCacheDir(), "ACache"); + return get(f, max_zise, max_count); + } catch (Exception ignored) { + } + + return null; + } + + public static ACache get(File cacheDir, long max_zise, int max_count) { + try { + ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid()); + if (manager == null) { + manager = new ACache(cacheDir, max_zise, max_count); + mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager); + } + return manager; + } catch (Exception ignored) { + } + + return null; + } + + private static String myPid() { + return "_" + android.os.Process.myPid(); + } + + // ======================================= + // ============ String数据 读写 ============== + // ======================================= + + /** + * 保存 String数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的String数据 + */ + public void put(String key, String value) { + try { + File file = mCache.newFile(key); + BufferedWriter out = null; + try { + out = new BufferedWriter(new FileWriter(file), 1024); + out.write(value); + } catch (IOException ignored) { + } finally { + if (out != null) { + try { + out.flush(); + out.close(); + } catch (IOException ignored) { + } + } + mCache.put(file); + } + } catch (Exception ignored) { + } + + } + + /** + * 保存 String数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的String数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, String value, int saveTime) { + put(key, Utils.newStringWithDateInfo(saveTime, value)); + } + + /** + * 读取 String数据 + * @return String 数据 + */ + public String getAsString(String key) { + File file = mCache.get(key); + if (!file.exists()) + return null; + boolean removeFile = false; + try (BufferedReader in = new BufferedReader(new FileReader(file))) { + StringBuilder readString = new StringBuilder(); + String currentLine; + while ((currentLine = in.readLine()) != null) { + readString.append(currentLine); + } + if (!Utils.isDue(readString.toString())) { + return Utils.clearDateInfo(readString.toString()); + } else { + removeFile = true; + return null; + } + } catch (IOException e) { + return null; + } finally { + if (removeFile) + remove(key); + } + } + + // ======================================= + // ========== JSONObject 数据 读写 ========= + // ======================================= + + /** + * 保存 JSONObject数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的JSON数据 + */ + public void put(String key, JSONObject value) { + put(key, value.toString()); + } + + /** + * 保存 JSONObject数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的JSONObject数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, JSONObject value, int saveTime) { + put(key, value.toString(), saveTime); + } + + /** + * 读取JSONObject数据 + * @return JSONObject数据 + */ + public JSONObject getAsJSONObject(String key) { + String JSONString = getAsString(key); + try { + return new JSONObject(JSONString); + } catch (Exception e) { + return null; + } + } + + // ======================================= + // ============ JSONArray 数据 读写 ============= + // ======================================= + + /** + * 保存 JSONArray数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的JSONArray数据 + */ + public void put(String key, JSONArray value) { + put(key, value.toString()); + } + + /** + * 保存 JSONArray数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的JSONArray数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, JSONArray value, int saveTime) { + put(key, value.toString(), saveTime); + } + + /** + * 读取JSONArray数据 + * @return JSONArray数据 + */ + public JSONArray getAsJSONArray(String key) { + String JSONString = getAsString(key); + try { + return new JSONArray(JSONString); + } catch (Exception e) { + return null; + } + } + + // ======================================= + // ============== byte 数据 读写 ============= + // ======================================= + + /** + * 保存 byte数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的数据 + */ + public void put(String key, byte[] value) { + File file = mCache.newFile(key); + FileOutputStream out = null; + try { + out = new FileOutputStream(file); + out.write(value); + } catch (Exception ignored) { + } finally { + if (out != null) { + try { + out.flush(); + out.close(); + } catch (IOException ignored) { + } + } + mCache.put(file); + } + } + + /** + * 保存 byte数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, byte[] value, int saveTime) { + put(key, Utils.newByteArrayWithDateInfo(saveTime, value)); + } + + /** + * 获取 byte 数据 + * @return byte 数据 + */ + public byte[] getAsBinary(String key) { + RandomAccessFile RAFile = null; + boolean removeFile = false; + try { + File file = mCache.get(key); + if (!file.exists()) + return null; + RAFile = new RandomAccessFile(file, "r"); + byte[] byteArray = new byte[(int) RAFile.length()]; + RAFile.read(byteArray); + if (!Utils.isDue(byteArray)) { + return Utils.clearDateInfo(byteArray); + } else { + removeFile = true; + return null; + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + if (RAFile != null) { + try { + RAFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (removeFile) + remove(key); + } + } + + // ======================================= + // ============= 序列化 数据 读写 =============== + // ======================================= + + /** + * 保存 Serializable数据 到 缓存中 + * + * @param key 保存的key + * @param value 保存的value + */ + public void put(String key, Serializable value) { + put(key, value, -1); + } + + /** + * 保存 Serializable数据到 缓存中 + * + * @param key 保存的key + * @param value 保存的value + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, Serializable value, int saveTime) { + ByteArrayOutputStream baos; + baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(value); + byte[] data = baos.toByteArray(); + if (saveTime != -1) { + put(key, data, saveTime); + } else { + put(key, data); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 读取 Serializable数据 + * @return Serializable 数据 + */ + public Object getAsObject(String key) { + byte[] data = getAsBinary(key); + if (data != null) { + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + try { + bais = new ByteArrayInputStream(data); + ois = new ObjectInputStream(bais); + return ois.readObject(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + try { + if (bais != null) + bais.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + if (ois != null) + ois.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + + } + + // ======================================= + // ============== bitmap 数据 读写 ============= + // ======================================= + + /** + * 保存 bitmap 到 缓存中 + * + * @param key 保存的key + * @param value 保存的bitmap数据 + */ + public void put(String key, Bitmap value) { + put(key, Utils.Bitmap2Bytes(value)); + } + + /** + * 保存 bitmap 到 缓存中 + * + * @param key 保存的key + * @param value 保存的 bitmap 数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, Bitmap value, int saveTime) { + put(key, Utils.Bitmap2Bytes(value), saveTime); + } + + /** + * 读取 bitmap 数据 + * @return bitmap 数据 + */ + public Bitmap getAsBitmap(String key) { + if (getAsBinary(key) == null) { + return null; + } + return Utils.Bytes2Bimap(getAsBinary(key)); + } + + // ======================================= + // ============= drawable 数据 读写 ============= + // ======================================= + + /** + * 保存 drawable 到 缓存中 + * + * @param key 保存的key + * @param value 保存的drawable数据 + */ + public void put(String key, Drawable value) { + put(key, Utils.drawable2Bitmap(value)); + } + + /** + * 保存 drawable 到 缓存中 + * + * @param key 保存的key + * @param value 保存的 drawable 数据 + * @param saveTime 保存的时间,单位:秒 + */ + public void put(String key, Drawable value, int saveTime) { + put(key, Utils.drawable2Bitmap(value), saveTime); + } + + /** + * 读取 Drawable 数据 + * @return Drawable 数据 + */ + public Drawable getAsDrawable(String key) { + if (getAsBinary(key) == null) { + return null; + } + return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key))); + } + + /** + * 获取缓存文件 + * @return value 缓存的文件 + */ + public File file(String key) { + try { + File f = mCache.newFile(key); + if (f.exists()) { + return f; + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 移除某个key + * @return 是否移除成功 + */ + public boolean remove(String key) { + return mCache.remove(key); + } + + /** + * 清除所有数据 + */ + public void clear() { + mCache.clear(); + } + + /** + * @author 杨福海(michael) www.yangfuhai.com + * @version 1.0 + * @title 时间计算工具类 + */ + private static class Utils { + + private static final char mSeparator = ' '; + + /** + * 判断缓存的String数据是否到期 + * @return true:到期了 false:还没有到期 + */ + private static boolean isDue(String str) { + return isDue(str.getBytes()); + } + + /** + * 判断缓存的byte数据是否到期 + * @return true:到期了 false:还没有到期 + */ + private static boolean isDue(byte[] data) { + try { + String[] strs = getDateInfoFromDate(data); + if (strs != null && strs.length == 2) { + String saveTimeStr = strs[0]; + while (saveTimeStr.startsWith("0")) { + saveTimeStr = saveTimeStr + .substring(1); + } + long saveTime = Long.valueOf(saveTimeStr); + long deleteAfter = Long.valueOf(strs[1]); + if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) { + return true; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return false; + } + + private static String newStringWithDateInfo(int second, String strInfo) { + return createDateInfo(second) + strInfo; + } + + private static byte[] newByteArrayWithDateInfo(int second, byte[] data2) { + byte[] data1 = createDateInfo(second).getBytes(); + byte[] retdata = new byte[data1.length + data2.length]; + System.arraycopy(data1, 0, retdata, 0, data1.length); + System.arraycopy(data2, 0, retdata, data1.length, data2.length); + return retdata; + } + + private static String clearDateInfo(String strInfo) { + if (strInfo != null && hasDateInfo(strInfo.getBytes())) { + strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1); + } + return strInfo; + } + + private static byte[] clearDateInfo(byte[] data) { + if (hasDateInfo(data)) { + return copyOfRange(data, indexOf(data, mSeparator) + 1, + data.length); + } + return data; + } + + private static boolean hasDateInfo(byte[] data) { + return data != null && data.length > 15 && data[13] == '-' + && indexOf(data, mSeparator) > 14; + } + + private static String[] getDateInfoFromDate(byte[] data) { + if (hasDateInfo(data)) { + String saveDate = new String(copyOfRange(data, 0, 13)); + String deleteAfter = new String(copyOfRange(data, 14, + indexOf(data, mSeparator))); + return new String[]{saveDate, deleteAfter}; + } + return null; + } + + private static int indexOf(byte[] data, char c) { + for (int i = 0; i < data.length; i++) { + if (data[i] == c) { + return i; + } + } + return -1; + } + + private static byte[] copyOfRange(byte[] original, int from, int to) { + int newLength = to - from; + if (newLength < 0) + throw new IllegalArgumentException(from + " > " + to); + byte[] copy = new byte[newLength]; + System.arraycopy(original, from, copy, 0, + Math.min(original.length - from, newLength)); + return copy; + } + + private static String createDateInfo(int second) { + StringBuilder currentTime = new StringBuilder(System.currentTimeMillis() + ""); + while (currentTime.length() < 13) { + currentTime.insert(0, "0"); + } + return currentTime + "-" + second + mSeparator; + } + + /* + * Bitmap → byte[] + */ + private static byte[] Bitmap2Bytes(Bitmap bm) { + if (bm == null) { + return null; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + bm.compress(Bitmap.CompressFormat.PNG, 100, baos); + return baos.toByteArray(); + } + + /* + * byte[] → Bitmap + */ + private static Bitmap Bytes2Bimap(byte[] b) { + if (b.length == 0) { + return null; + } + return BitmapFactory.decodeByteArray(b, 0, b.length); + } + + /* + * Drawable → Bitmap + */ + private static Bitmap drawable2Bitmap(Drawable drawable) { + if (drawable == null) { + return null; + } + // 取 drawable 的长宽 + int w = drawable.getIntrinsicWidth(); + int h = drawable.getIntrinsicHeight(); + // 取 drawable 的颜色格式 + Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 + : Bitmap.Config.RGB_565; + // 建立对应 bitmap + Bitmap bitmap = Bitmap.createBitmap(w, h, config); + // 建立对应 bitmap 的画布 + Canvas canvas = new Canvas(bitmap); + drawable.setBounds(0, 0, w, h); + // 把 drawable 内容画到画布中 + drawable.draw(canvas); + return bitmap; + } + + /* + * Bitmap → Drawable + */ + @SuppressWarnings("deprecation") + private static Drawable bitmap2Drawable(Bitmap bm) { + if (bm == null) { + return null; + } + return new BitmapDrawable(bm); + } + } + + /** + * @author 杨福海(michael) www.yangfuhai.com + * @version 1.0 + * @title 缓存管理器 + */ + public class ACacheManager { + private final AtomicLong cacheSize; + private final AtomicInteger cacheCount; + private final long sizeLimit; + private final int countLimit; + private final Map lastUsageDates = Collections + .synchronizedMap(new HashMap<>()); + protected File cacheDir; + + private ACacheManager(File cacheDir, long sizeLimit, int countLimit) { + this.cacheDir = cacheDir; + this.sizeLimit = sizeLimit; + this.countLimit = countLimit; + cacheSize = new AtomicLong(); + cacheCount = new AtomicInteger(); + calculateCacheSizeAndCacheCount(); + } + + /** + * 计算 cacheSize和cacheCount + */ + private void calculateCacheSizeAndCacheCount() { + new Thread(() -> { + + try { + int size = 0; + int count = 0; + File[] cachedFiles = cacheDir.listFiles(); + if (cachedFiles != null) { + for (File cachedFile : cachedFiles) { + size += calculateSize(cachedFile); + count += 1; + lastUsageDates.put(cachedFile, + cachedFile.lastModified()); + } + cacheSize.set(size); + cacheCount.set(count); + } + } catch (Exception e) { + e.printStackTrace(); + } + + }).start(); + } + + private void put(File file) { + + try { + int curCacheCount = cacheCount.get(); + while (curCacheCount + 1 > countLimit) { + long freedSize = removeNext(); + cacheSize.addAndGet(-freedSize); + + curCacheCount = cacheCount.addAndGet(-1); + } + cacheCount.addAndGet(1); + + long valueSize = calculateSize(file); + long curCacheSize = cacheSize.get(); + while (curCacheSize + valueSize > sizeLimit) { + long freedSize = removeNext(); + curCacheSize = cacheSize.addAndGet(-freedSize); + } + cacheSize.addAndGet(valueSize); + + long currentTime = System.currentTimeMillis(); + file.setLastModified(currentTime); + lastUsageDates.put(file, currentTime); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private File get(String key) { + File file = newFile(key); + long currentTime = System.currentTimeMillis(); + file.setLastModified(currentTime); + lastUsageDates.put(file, currentTime); + + return file; + } + + private File newFile(String key) { + return new File(cacheDir, key.hashCode() + ""); + } + + private boolean remove(String key) { + File image = get(key); + return image.delete(); + } + + private void clear() { + try { + lastUsageDates.clear(); + cacheSize.set(0); + File[] files = cacheDir.listFiles(); + if (files != null) { + for (File f : files) { + f.delete(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * 移除旧的文件 + */ + private long removeNext() { + try { + if (lastUsageDates.isEmpty()) { + return 0; + } + + Long oldestUsage = null; + File mostLongUsedFile = null; + Set> entries = lastUsageDates.entrySet(); + synchronized (lastUsageDates) { + for (Entry entry : entries) { + if (mostLongUsedFile == null) { + mostLongUsedFile = entry.getKey(); + oldestUsage = entry.getValue(); + } else { + Long lastValueUsage = entry.getValue(); + if (lastValueUsage < oldestUsage) { + oldestUsage = lastValueUsage; + mostLongUsedFile = entry.getKey(); + } + } + } + } + + long fileSize = 0; + if (mostLongUsedFile != null) { + fileSize = calculateSize(mostLongUsedFile); + if (mostLongUsedFile.delete()) { + lastUsageDates.remove(mostLongUsedFile); + } + } + return fileSize; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + + + } + + private long calculateSize(File file) { + return file.length(); + } + } + +} diff --git a/app/src/main/java/io/legado/book/view/MainActivity.kt b/app/src/main/java/io/legado/book/view/MainActivity.kt new file mode 100644 index 000000000..c62cc4270 --- /dev/null +++ b/app/src/main/java/io/legado/book/view/MainActivity.kt @@ -0,0 +1,94 @@ +package io.legado.book.view + +import android.os.Bundle +import com.google.android.material.floatingactionbutton.FloatingActionButton +import com.google.android.material.snackbar.Snackbar +import androidx.core.view.GravityCompat +import androidx.appcompat.app.ActionBarDrawerToggle +import android.view.MenuItem +import androidx.drawerlayout.widget.DrawerLayout +import com.google.android.material.navigation.NavigationView +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar +import android.view.Menu +import io.legado.book.R + +class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { + + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + val toolbar: Toolbar = findViewById(R.id.toolbar) + setSupportActionBar(toolbar) + + val fab: FloatingActionButton = findViewById(R.id.fab) + fab.setOnClickListener { view -> + Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) + .setAction("Action", null).show() + } + val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) + val navView: NavigationView = findViewById(R.id.nav_view) + val toggle = ActionBarDrawerToggle( + this, drawerLayout, toolbar, + R.string.navigation_drawer_open, + R.string.navigation_drawer_close + ) + drawerLayout.addDrawerListener(toggle) + toggle.syncState() + + navView.setNavigationItemSelectedListener(this) + } + + override fun onBackPressed() { + val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) + if (drawerLayout.isDrawerOpen(GravityCompat.START)) { + drawerLayout.closeDrawer(GravityCompat.START) + } else { + super.onBackPressed() + } + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + // Inflate the menu; this adds items to the action bar if it is present. + menuInflater.inflate(R.menu.main, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + return when (item.itemId) { + R.id.action_settings -> true + else -> super.onOptionsItemSelected(item) + } + } + + override fun onNavigationItemSelected(item: MenuItem): Boolean { + // Handle navigation view item clicks here. + when (item.itemId) { + R.id.nav_home -> { + // Handle the camera action + } + R.id.nav_gallery -> { + + } + R.id.nav_slideshow -> { + + } + R.id.nav_tools -> { + + } + R.id.nav_share -> { + + } + R.id.nav_send -> { + + } + } + val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) + drawerLayout.closeDrawer(GravityCompat.START) + return true + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 000000000..6348baae3 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 000000000..a0ad202f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_menu_camera.xml b/app/src/main/res/drawable/ic_menu_camera.xml new file mode 100644 index 000000000..41688d5c0 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_camera.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/drawable/ic_menu_gallery.xml b/app/src/main/res/drawable/ic_menu_gallery.xml new file mode 100644 index 000000000..ff8ce529f --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_gallery.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu_manage.xml b/app/src/main/res/drawable/ic_menu_manage.xml new file mode 100644 index 000000000..a0e423cd0 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_manage.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_menu_send.xml b/app/src/main/res/drawable/ic_menu_send.xml new file mode 100644 index 000000000..9745066f1 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_send.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu_share.xml b/app/src/main/res/drawable/ic_menu_share.xml new file mode 100644 index 000000000..b3e39e222 --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_share.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu_slideshow.xml b/app/src/main/res/drawable/ic_menu_slideshow.xml new file mode 100644 index 000000000..ae51e494a --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_slideshow.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/side_nav_bar.xml b/app/src/main/res/drawable/side_nav_bar.xml new file mode 100644 index 000000000..a33798b60 --- /dev/null +++ b/app/src/main/res/drawable/side_nav_bar.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 000000000..ac9a123b3 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,26 @@ + + + + + + + + diff --git a/app/src/main/res/layout/app_bar_main.xml b/app/src/main/res/layout/app_bar_main.xml new file mode 100644 index 000000000..9f8165bb8 --- /dev/null +++ b/app/src/main/res/layout/app_bar_main.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml new file mode 100644 index 000000000..0ab422731 --- /dev/null +++ b/app/src/main/res/layout/content_main.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/nav_header_main.xml b/app/src/main/res/layout/nav_header_main.xml new file mode 100644 index 000000000..92ca611a4 --- /dev/null +++ b/app/src/main/res/layout/nav_header_main.xml @@ -0,0 +1,37 @@ + + + + + + + + + + diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml new file mode 100644 index 000000000..9df2b3d76 --- /dev/null +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml new file mode 100644 index 000000000..d579f6feb --- /dev/null +++ b/app/src/main/res/menu/main.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 000000000..bbd3e0212 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 000000000..bbd3e0212 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 000000000..898f3ed59 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 000000000..dffca3601 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 000000000..64ba76f75 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 000000000..dae5e0823 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 000000000..e5ed46597 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 000000000..14ed0af35 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 000000000..b0907cac3 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 000000000..d8ae03154 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 000000000..2c18de9e6 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 000000000..beed3cdd2 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml new file mode 100644 index 000000000..e54680451 --- /dev/null +++ b/app/src/main/res/values-v21/styles.xml @@ -0,0 +1,7 @@ + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 000000000..69b22338c --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #008577 + #00574B + #D81B60 + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 000000000..4ab4520ff --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,8 @@ + + + 16dp + 16dp + 8dp + 176dp + 16dp + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 000000000..6f0dca604 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,16 @@ + + 阅读 + Open navigation drawer + Close navigation drawer + Android Studio + android.studio@android.com + Navigation header + Settings + + Home + Gallery + Slideshow + Tools + Share + Send + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 000000000..16dbab30f --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,17 @@ + + + + + +