/* * 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 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 getSearchHistoryList() { List 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 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()); } } }