@Override public boolean init(final Context context, final AttributeSet attrs) { if (!super.init(context, attrs)) { return false; } final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AwesomePreference); int filePath = -1, filePathList = -1, fileValue = -1; if (a != null) { filePath = a.getResourceId(R.styleable.AwesomePreference_filePath, -1); filePathList = a.getResourceId(R.styleable.AwesomePreference_filePathList, -1); fileValue = a.getResourceId(R.styleable.AwesomePreference_fileValue, -1); mStartup = a.getBoolean(R.styleable.AwesomePreference_startup, mStartup); mMultiFile = a.getBoolean(R.styleable.AwesomePreference_multifile, mMultiFile); mValueChecked = a.getString(R.styleable.AwesomePreference_valueChecked); mValueNotChecked = a.getString(R.styleable.AwesomePreference_valueNotChecked); a.recycle(); } final Resources res = context.getResources(); if (filePath != -1) { mPath = Utils.checkPath(res.getString(filePath)); mPaths = null; } else if (filePathList != -1) { mPaths = res.getStringArray(filePathList); mPath = Utils.checkPaths(mPaths); if (mPath.isEmpty() || !mMultiFile) { mPaths = null; } } else { mPath = ""; mPaths = null; } if (!TextUtils.isEmpty(mPath) && filePathList != -1 && fileValue != -1) { final int index = Arrays.asList(res.getStringArray(filePathList)).indexOf(mPath); final String[] values = res.getStringArray(fileValue)[index].split(";"); mValueChecked = values[0]; mValueNotChecked = values[1]; Logger.d( this, "mValueChecked -> %s\nmValueNotChecked -> %s", mValueChecked, mValueNotChecked); } if (TextUtils.isEmpty(mValueChecked)) { mValueChecked = "1"; } if (TextUtils.isEmpty(mValueNotChecked)) { mValueNotChecked = "0"; } handleSelf(true); return true; }
public static String restore(BootupConfig config) { final boolean hasVdd = Utils.fileExists(VoltageUtils.VDD_TABLE_FILE); final boolean hasUv = Utils.fileExists(VoltageUtils.UV_TABLE_FILE); if (!hasVdd && !hasUv) { return ""; } final ArrayList<BootupItem> bootupItems = config.getItemsByCategory(BootupConfig.CATEGORY_VOLTAGE); if (bootupItems.size() == 0) { return ""; } final BootupItem voltageBootupItem = bootupItems.get(0); if (voltageBootupItem == null || !voltageBootupItem.enabled) { return ""; } final StringBuilder restore = new StringBuilder(); if (hasVdd) { final String value = ExtraConfig.get().vdd; Logger.v(VoltageFragment.class, "VDD Table: " + value); if (!TextUtils.isEmpty(value)) { final String[] values = value.split("XXX"); for (final String s : values) { restore.append(Utils.getWriteCommand(VoltageUtils.VDD_TABLE_FILE, s)); } } } else { final String value = ExtraConfig.get().uv; Logger.v(VoltageFragment.class, "UV Table: " + value); if (!TextUtils.isEmpty(value)) { restore.append(Utils.getWriteCommand(VoltageUtils.UV_TABLE_FILE, value)); } } return restore.toString(); }
@Override protected String doInBackground(String... params) { try { mNames = VoltageUtils.get().getUvValues(true); mValues = VoltageUtils.get().getUvValues(false); } catch (Exception exc) { Logger.e(this, "UV ERROR: " + exc.getMessage()); return "ERROR"; } Logger.v(this, "UV TABLE: " + buildTable(mValues)); String name; CustomPreference pref; final int length = mNames.length; for (int i = 0; i < length; i++) { final int j = i; name = mNames[i]; pref = new CustomPreference(getActivity()); pref.setTitle(name); pref.areMilliVolts(millivolts); if (isVdd) { pref.setSummary(mValues[i]); } else { pref.setSummary(mValues[i] + " mV"); } pref.setKey(mValues[i]); mCategory.addPreference(pref); pref.setOnPreferenceClickListener( new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(final Preference p) { final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final LinearLayout ll = new LinearLayout(getActivity()); ll.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT)); final EditText et = new EditText(getActivity()); final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(40, 40, 40, 40); params.gravity = Gravity.CENTER; final String val = p.getKey(); et.setLayoutParams(params); et.setRawInputType(InputType.TYPE_CLASS_NUMBER); et.setGravity(Gravity.CENTER_HORIZONTAL); et.setText(val); ll.addView(et); builder.setView(ll); builder.setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (et.getText() != null) { if (isVdd) { final String value = p.getTitle() + " " + et.getText().toString(); Utils.writeValue(VoltageUtils.UV_TABLE_FILE, value); p.setSummary(et.getText().toString()); p.setKey(et.getText().toString()); mValues[j] = p.getKey(); } else { final String value = et.getText().toString(); p.setSummary(value + " mV"); p.setKey(value); mValues[j] = value; Utils.writeValue(VoltageUtils.UV_TABLE_FILE, buildTable(mValues)); } } } }); final AlertDialog dialog = builder.create(); dialog.show(); final Window window = dialog.getWindow(); window.setLayout(800, LayoutParams.WRAP_CONTENT); return true; } }); } return "Executed"; }