From 13defd5a8a3624bfe999733d3add8b96994faade Mon Sep 17 00:00:00 2001 From: axlecho Date: Thu, 24 Mar 2016 23:01:34 +0800 Subject: [PATCH 01/19] for my setting --- gradle.properties | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/gradle.properties b/gradle.properties index 1d3591c8a..5097a6c01 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,18 +1,17 @@ -# Project-wide Gradle settings. - -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. - -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html - -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. -# Default value: -Xmx10248m -XX:MaxPermSize=256m -# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 - -# When configured, Gradle will run in incubating parallel mode. -# This option should only be used with decoupled projects. More details, visit -# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +## Project-wide Gradle settings. +# +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +# +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +#Thu Mar 24 00:22:37 CST 2016 +systemProp.http.proxyHost=127.0.0.1 +systemProp.http.proxyPort=1080 From 1feaf6bca188857d5ec4f43eea2392c89b048662 Mon Sep 17 00:00:00 2001 From: axlecho Date: Fri, 25 Mar 2016 00:47:33 +0800 Subject: [PATCH 02/19] add dict test --- app/src/main/AndroidManifest.xml | 57 ++++-- .../java/com/hippo/dict/DictDatabase.java | 181 ++++++++++++++++++ .../hippo/ehviewer/ui/DictImportActivity.java | 31 +++ .../main/res/layout/activity_dict_improt.xml | 12 ++ dict/demo.ehdict | 21 ++ 5 files changed, 282 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/hippo/dict/DictDatabase.java create mode 100644 app/src/main/java/com/hippo/ehviewer/ui/DictImportActivity.java create mode 100644 app/src/main/res/layout/activity_dict_improt.xml create mode 100644 dict/demo.ehdict diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 057100a6f..911619b29 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - - + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.hippo.ehviewer"> - - - + + + - + + + + + + + + + + + + + @@ -132,42 +149,42 @@ + android:theme="@style/AppTheme" /> + android:label="@string/excluded_languages" + android:theme="@style/AppTheme.NoActionBar" /> + android:label="@string/dir_picker" + android:theme="@style/AppTheme.NoActionBar" /> + android:label="@string/license" + android:theme="@style/AppTheme.NoActionBar" /> + android:label="@string/filter" + android:theme="@style/AppTheme.NoActionBar" /> + android:label="@string/set_pattern_protection" + android:theme="@style/AppTheme.NoActionBar" /> + android:label="@string/download_service_label" /> diff --git a/app/src/main/java/com/hippo/dict/DictDatabase.java b/app/src/main/java/com/hippo/dict/DictDatabase.java new file mode 100644 index 000000000..aeb517809 --- /dev/null +++ b/app/src/main/java/com/hippo/dict/DictDatabase.java @@ -0,0 +1,181 @@ +package com.hippo.dict; + +/* + * Copyright (C) 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.text.TextUtils; +import android.util.JsonReader; + +import com.hippo.util.SqlUtils; +import com.hippo.yorozuya.Say; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.LinkedList; +import java.util.List; + + +public class DictDatabase { + + private static final String TAG = DictDatabase.class.getSimpleName(); + + public static final String COLUMN_PARENT = "parent"; + public static final String COLUMN_DATA = "data"; + public static final String COLUMN_DICT = "dict"; + + private static final String DATABASE_NAME = "search_database.db"; + private static final String TABLE_DICT = "dict"; + private static final String SEPARATOR = "@@@"; + private SQLiteDatabase mDatabase; + + private static DictDatabase sInstance; + + public static DictDatabase getInstance(Context context) { + if (sInstance == null) { + sInstance = new DictDatabase(context.getApplicationContext()); + } + return sInstance; + } + + private DictDatabase(Context context) { + DatabaseHelper databaseHelper = new DatabaseHelper(context); + mDatabase = databaseHelper.getWritableDatabase(); + } + + public String[] getSuggestions(String prefix) { + List queryList = new LinkedList<>(); + + // TODO add limit + if (TextUtils.isEmpty(prefix)) { + return queryList.toArray(new String[queryList.size()]); + } + + StringBuilder sb = new StringBuilder(); + sb.append("SELECT * FROM ").append(TABLE_DICT); + sb.append(" WHERE ").append(COLUMN_DATA).append(" LIKE '") + .append("%").append(SEPARATOR) + .append(SqlUtils.sqlEscapeString(prefix)) + .append(SEPARATOR).append("%'") + .append(" LIMIT 5"); + Cursor cursor = mDatabase.rawQuery(sb.toString(), null); + + int queryIndex = cursor.getColumnIndex(COLUMN_DATA); + if (cursor.moveToFirst()) { + while (!cursor.isAfterLast()) { + String data = cursor.getString(queryIndex); + String datas[] = data.split(SEPARATOR); + queryList.add(data); + cursor.moveToNext(); + } + } + cursor.close(); + return queryList.toArray(new String[queryList.size()]); + } + + public void importDict(final String dictPath) throws IOException { + FileInputStream fileInputStream = new FileInputStream(dictPath); + JsonReader jsonReader = new JsonReader(new InputStreamReader( + fileInputStream, "UTF-8")); + jsonReader.beginObject(); + while (jsonReader.hasNext()) { + Say.d(TAG, jsonReader.nextString()); + } + jsonReader.endObject(); + jsonReader.close(); + } + + public void addQuery(String data, String parent, String dict) { + // Delete old first + deleteQuery(data); + // Add it to database + ContentValues values = new ContentValues(); + values.put(COLUMN_DATA, data); + values.put(COLUMN_PARENT, parent); + values.put(COLUMN_DICT, dict); + mDatabase.insert(TABLE_DICT, null, values); + } + + public void deleteQuery(final String query) { + // mDatabase.delete(TABLE_DICT, COLUMN_QUERY + "=?", new String[]{query}); + } + + public void clearQuery() { + truncateHistory(0); + } + + /** + * Reduces the length of the history table, to prevent it from growing too large. + * + * @param maxEntries Max entries to leave in the table. 0 means remove all entries. + */ + protected void truncateHistory(int maxEntries) { + if (maxEntries < 0) { + throw new IllegalArgumentException(); + } + + try { + // null means "delete all". otherwise "delete but leave n newest" + String selection = null; + if (maxEntries > 0) { + selection = "_id IN " + + "(SELECT _id FROM " + TABLE_DICT + + " ORDER BY " + COLUMN_DATA + " DESC" + + " LIMIT -1 OFFSET " + String.valueOf(maxEntries) + ")"; + } + mDatabase.delete(TABLE_DICT, selection, null); + } catch (RuntimeException e) { + Say.e(TAG, "truncateHistory", e); + } + } + + + /** + * Builds the database. This version has extra support for using the version field + * as a mode flags field, and configures the database columns depending on the mode bits + * (features) requested by the extending class. + */ + private static class DatabaseHelper extends SQLiteOpenHelper { + + public DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, 1); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + TABLE_DICT + " (" + + "_id INTEGER PRIMARY KEY" + + "," + COLUMN_DATA + " TEXT" + + "," + COLUMN_PARENT + " TEXT" + + "," + COLUMN_DICT + " TEXT" + + ");"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE_DICT); + onCreate(db); + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/DictImportActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/DictImportActivity.java new file mode 100644 index 000000000..6b7d17210 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/DictImportActivity.java @@ -0,0 +1,31 @@ +package com.hippo.ehviewer.ui; + +import android.os.Bundle; +import android.support.annotation.LayoutRes; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; + +import com.hippo.ehviewer.R; +import com.hippo.yorozuya.Say; + +public class DictImportActivity extends AppCompatActivity { + + private final static String TAG = DictImportActivity.class.getSimpleName(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Log.d(TAG, getIntent().getData().toString()); + setContentView(R.layout.activity_dict_improt); + Button importBtn = (Button) findViewById(R.id.btn_import); + importBtn.setText(importBtn.getText() + getIntent().getData().toString()); + importBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + } + }); + } +} diff --git a/app/src/main/res/layout/activity_dict_improt.xml b/app/src/main/res/layout/activity_dict_improt.xml new file mode 100644 index 000000000..deff421b7 --- /dev/null +++ b/app/src/main/res/layout/activity_dict_improt.xml @@ -0,0 +1,12 @@ + + + +