Foxit PDF for Android保存getCenterItems()获取到的数据格式,通过setCenterItems恢复设置。
- 支持通过读取json来设置
- 持久化保存工具栏配置
- 恢复工具栏配置
package com.foxit.pdfreader.fragment;
import static com.foxit.uiextensions.utils.AppUtil.getApplicationContext;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import com.foxit.uiextensions.UIExtensionsManager;
import com.foxit.uiextensions.controls.toolbar.ToolItemBean;
import com.foxit.uiextensions.controls.toolbar.ToolProperty;
import com.foxit.uiextensions.pdfreader.MainCenterItemBean;
import com.foxit.uiextensions.utils.AppSharedPreferences;
import com.foxit.uiextensions.utils.AppUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class CenterItemsManager {
public static final String SP_NAME = "Foxit_Plugin_SP";
public static final String TAB_ITEMS_KEY = "KEY_TAB_ITEMS";
/**
* 读取assets目录下的json文件
*
* @param fileName json文件Name
* @param context 系统上下文
* @return json字符串
*/
public static String getJson(String fileName, Context context) {
//将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
AssetManager assetManager = context.getAssets();
//通过管理器打开文件并读取
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
/**
* 保存TabItems
*
* @param items getMainFrame().getCenterItems() 配置
*/
public static void saveTabItems(List<MainCenterItemBean> items) {
try {
JSONObject rootObj = new JSONObject();
JSONArray centerItemsObj = new JSONArray();
rootObj.put("centerItems", centerItemsObj);
for (MainCenterItemBean centerItem : items) {
JSONObject centerObj = new JSONObject();
centerObj.put("type", centerItem.type);
centerObj.put("position", centerItem.position);
if (centerItem.toolItems != null) {
JSONArray toolItemsObj = new JSONArray();
centerObj.put("toolItems", toolItemsObj);
for (ToolItemBean toolItem : centerItem.toolItems) {
JSONObject toolObj = new JSONObject();
toolObj.put("itemStyle", toolItem.itemStyle);
toolObj.put("type", toolItem.type);
{
ToolProperty property = toolItem.property;
JSONObject propObj = new JSONObject();
if (property != null) {
setToolPropObjVal(propObj, property);
}
toolObj.put("property", propObj);
}
toolItemsObj.put(toolObj);
}
}
centerItemsObj.put(centerObj);
}
Log.d("ddd", rootObj.toString());
AppSharedPreferences.getInstance(getApplicationContext()).setString(SP_NAME, TAB_ITEMS_KEY, rootObj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 恢复设置好的tabItems
*
* @param mUiExtensionsManager UiExtensionsManager
*/
public static void restoreItems(UIExtensionsManager mUiExtensionsManager, String tabItems) {
if (!AppUtil.isEmpty(tabItems)) {
try {
JSONObject rootObj = new JSONObject(tabItems);
JSONArray centerItemsObj = rootObj.getJSONArray("centerItems");
ArrayList<MainCenterItemBean> items = new ArrayList<>();
for (int i = 0; i < centerItemsObj.length(); i++) {
JSONObject centerObj = centerItemsObj.getJSONObject(i);
MainCenterItemBean centerItem = new MainCenterItemBean();
centerItem.type = centerObj.getInt("type");
centerItem.position = centerObj.getInt("position");
if (!centerObj.has("toolItems")) {
items.add(centerItem);
continue;
}
centerItem.toolItems = new ArrayList<>();
JSONArray toolItemsObj = centerObj.getJSONArray("toolItems");
for (int toolIndex = 0; toolIndex < toolItemsObj.length(); toolIndex++) {
JSONObject toolObj = toolItemsObj.getJSONObject(toolIndex);
ToolItemBean toolItem = new ToolItemBean();
toolItem.itemStyle = toolObj.getInt("itemStyle");
toolItem.type = toolObj.getInt("type");
toolItem.property = new ToolProperty();
{
ToolProperty property = toolItem.property;
JSONObject propObj = toolObj.getJSONObject("property");
if (!setToolPropVal(property, propObj)) {
toolItem.property = null;
}
}
centerItem.toolItems.add(toolItem);
}
items.add(centerItem);
}
mUiExtensionsManager.getMainFrame().setCenterItems(items);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 设置工具属性值
*
* @param property 工具属性
* @param propObj JSON对象
* @return 是否设置了属性
*/
static boolean setToolPropVal(ToolProperty property, JSONObject propObj) {
try {
boolean haveProp = false;
if (propObj.has("type")) {
property.type = propObj.getInt("type");
haveProp = true;
}
if (propObj.has("color")) {
property.color = propObj.getInt("color");
haveProp = true;
}
if (propObj.has("fillColor")) {
property.fillColor = propObj.getInt("fillColor");
haveProp = true;
}
if (propObj.has("opacity")) {
property.opacity = propObj.getInt("opacity");
haveProp = true;
}
if (propObj.has("style")) {
property.style = propObj.getInt("style");
haveProp = true;
}
if (propObj.has("rotation")) {
property.rotation = propObj.getInt("rotation");
haveProp = true;
}
if (propObj.has("lineWidth")) {
property.lineWidth = (float) propObj.getDouble("lineWidth");
haveProp = true;
}
if (propObj.has("fontSize")) {
property.fontSize = (float) propObj.getDouble("fontSize");
haveProp = true;
}
if (propObj.has("fontName")) {
property.fontName = propObj.getString("fontName");
haveProp = true;
}
if (propObj.has("scaleFromUnitIndex")) {
property.scaleFromUnitIndex = propObj.getInt("scaleFromUnitIndex");
haveProp = true;
}
if (propObj.has("scaleToUnitIndex")) {
property.scaleToUnitIndex = propObj.getInt("scaleToUnitIndex");
haveProp = true;
}
if (propObj.has("scaleFromValue")) {
property.scaleFromValue = BigDecimal.valueOf(propObj.getDouble("scaleFromValue")).floatValue();
haveProp = true;
}
if (propObj.has("scaleToValue")) {
property.scaleToValue = BigDecimal.valueOf(propObj.getDouble("scaleToValue")).floatValue();
haveProp = true;
}
if (propObj.has("eraserShape")) {
property.eraserShape = propObj.getInt("eraserShape");
haveProp = true;
}
if (propObj.has("tag")) {
property.mTag = propObj.get("tag");
haveProp = true;
}
return haveProp;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 设置工具属性对象值
*
* @param propObj 工具属性对象
* @param property 工具属性
*/
static void setToolPropObjVal(JSONObject propObj, ToolProperty property) {
try {
propObj.put("type", property.type);
propObj.put("color", property.color);
propObj.put("fillColor", property.fillColor);
propObj.put("opacity", property.opacity);
propObj.put("style", property.style);
propObj.put("rotation", property.rotation);
propObj.put("lineWidth", property.lineWidth);
propObj.put("fontSize", property.fontSize);
propObj.put("fontName", property.fontName);
propObj.put("scaleFromUnitIndex", property.scaleFromUnitIndex);
propObj.put("scaleToUnitIndex", property.scaleToUnitIndex);
propObj.put("scaleFromValue", property.scaleFromValue);
propObj.put("scaleToValue", property.scaleToValue);
propObj.put("eraserShape", property.eraserShape);
if (property.mTag != null)
propObj.put("tag", property.mTag);
} catch (Exception e) {
e.printStackTrace();
}
}
}