Compare commits
24 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
e467aa564c | 2 weeks ago |
|
|
3f936d0461 | 2 weeks ago |
|
|
38f26b4f9c | 2 weeks ago |
|
|
12fd1b8542 | 4 weeks ago |
|
|
371cca116b | 4 weeks ago |
|
|
8b7d46a312 | 1 month ago |
|
|
3f97e8adde | 1 month ago |
|
|
9f1ac52c96 | 1 month ago |
|
|
941bbddb0b | 1 month ago |
|
|
a019855db4 | 1 month ago |
|
|
4f9254d2e7 | 1 month ago |
|
|
5b449a122b | 1 month ago |
|
|
0b245b3666 | 1 month ago |
|
|
f6d4acb2a2 | 1 month ago |
|
|
47df2fd159 | 1 month ago |
|
|
cf5e75b1da | 1 month ago |
|
|
cdcea59d78 | 1 month ago |
|
|
0b08fabd76 | 2 months ago |
|
|
37170a8286 | 2 months ago |
|
|
a71b02febe | 2 months ago |
|
|
4413e65a33 | 2 months ago |
|
|
6fdef61c5d | 2 months ago |
|
|
8ff06c9755 | 2 months ago |
|
|
e7aca19601 | 2 months ago |
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.micode.notes.tool;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SearchHistoryManager - 搜索历史记录管理类
|
||||||
|
* 使用SharedPreferences存储搜索历史记录
|
||||||
|
*/
|
||||||
|
public class SearchHistoryManager {
|
||||||
|
private static final String TAG = "SearchHistoryManager";
|
||||||
|
|
||||||
|
// SharedPreferences文件名
|
||||||
|
private static final String PREFERENCE_NAME = "search_history";
|
||||||
|
|
||||||
|
// 搜索历史键
|
||||||
|
private static final String KEY_SEARCH_HISTORY = "search_history";
|
||||||
|
|
||||||
|
// 最大历史记录数量
|
||||||
|
private static final int MAX_HISTORY_COUNT = 10;
|
||||||
|
|
||||||
|
// 单例实例
|
||||||
|
private static SearchHistoryManager sInstance;
|
||||||
|
|
||||||
|
// SharedPreferences实例
|
||||||
|
private SharedPreferences mSharedPreferences;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私有构造函数
|
||||||
|
* @param context 上下文
|
||||||
|
*/
|
||||||
|
private SearchHistoryManager(Context context) {
|
||||||
|
mSharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例实例
|
||||||
|
* @param context 上下文
|
||||||
|
* @return SearchHistoryManager实例
|
||||||
|
*/
|
||||||
|
public static synchronized SearchHistoryManager getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new SearchHistoryManager(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存搜索关键词到历史记录
|
||||||
|
* @param keyword 搜索关键词
|
||||||
|
*/
|
||||||
|
public void saveSearchKeyword(String keyword) {
|
||||||
|
if (TextUtils.isEmpty(keyword)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取现有历史记录
|
||||||
|
List<String> historyList = getSearchHistoryList();
|
||||||
|
|
||||||
|
// 如果已存在,移除旧的位置
|
||||||
|
if (historyList.contains(keyword)) {
|
||||||
|
historyList.remove(keyword);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加到最前面
|
||||||
|
historyList.add(0, keyword);
|
||||||
|
|
||||||
|
// 限制历史记录数量
|
||||||
|
if (historyList.size() > MAX_HISTORY_COUNT) {
|
||||||
|
historyList = historyList.subList(0, MAX_HISTORY_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存到SharedPreferences
|
||||||
|
saveHistoryList(historyList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取搜索历史记录列表
|
||||||
|
* @return 搜索历史记录列表
|
||||||
|
*/
|
||||||
|
public List<String> getSearchHistoryList() {
|
||||||
|
List<String> historyList = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
String historyJson = mSharedPreferences.getString(KEY_SEARCH_HISTORY, "[]");
|
||||||
|
JSONArray jsonArray = new JSONArray(historyJson);
|
||||||
|
|
||||||
|
for (int i = 0; i < jsonArray.length(); i++) {
|
||||||
|
historyList.add(jsonArray.getString(i));
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG, "Failed to parse search history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return historyList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除所有搜索历史记录
|
||||||
|
*/
|
||||||
|
public void clearSearchHistory() {
|
||||||
|
mSharedPreferences.edit().remove(KEY_SEARCH_HISTORY).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存历史记录列表到SharedPreferences
|
||||||
|
* @param historyList 历史记录列表
|
||||||
|
*/
|
||||||
|
private void saveHistoryList(List<String> historyList) {
|
||||||
|
try {
|
||||||
|
JSONArray jsonArray = new JSONArray(historyList);
|
||||||
|
mSharedPreferences.edit().putString(KEY_SEARCH_HISTORY, jsonArray.toString()).apply();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to save search history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 245 B After Width: | Height: | Size: 245 B |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 443 B After Width: | Height: | Size: 443 B |
|
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 5.0 KiB |
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 6.7 KiB After Width: | Height: | Size: 6.7 KiB |
|
Before Width: | Height: | Size: 554 KiB After Width: | Height: | Size: 554 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 4.4 KiB After Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 4.4 KiB After Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 88 KiB |
|
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 4.8 KiB After Width: | Height: | Size: 4.8 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 5.8 KiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |