Permalink
Browse files

optimized SysCommand by trying to read/write sysfs directly without SU

if we have permission
and remove some unused code
  • Loading branch information...
arifhn committed Jan 14, 2012
1 parent 8685853 commit 81a4a87655b7884038bbfa78f90ae49056438827
View
@@ -185,17 +185,6 @@ case "$?" in
;;
esac
-# lazy screenoff
-STAT=`cat /sys/devices/system/cpu/cpufreq/lazy/screenoff_maxfreq`
-case "$?" in
- 0)
- echo "key_screenoff_maxfreq=$STAT" >> $OUT_FILE
- ;;
- *)
- echo "key_screenoff_maxfreq=-1" >> $OUT_FILE
- ;;
-esac
-
# iosched
STAT=`cat /sys/block/mmcblk0/queue/scheduler`
case "$?" in
@@ -73,7 +73,7 @@ private void reloadFrequencies() {
SysCommand sc = SysCommand.getInstance();
Integer availableFreqs[] = null;
String availableFreqsStr[] = null;
- int n = sc.suRun("cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies");
+ int n = sc.readSysfs("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies");
if(n > 0) {
String temp = sc.getLastResult(0);
String f[] = temp.split(" ");
@@ -85,7 +85,7 @@ private void reloadFrequencies() {
}
}else {
// try read available frequencies from time_in_state
- n = sc.suRun("cat", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
+ n = sc.readSysfs("/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
if(n >= 0) {
availableFreqs = new Integer[n];
availableFreqsStr = new String[n];
@@ -128,7 +128,7 @@ private void showIdleStatsDialog() {
Pattern time = Pattern.compile("([0-9]+)ms");
Pattern average = Pattern.compile("\\(([0-9]+)ms\\)");
- SysCommand.getInstance().suRun("cat", "/sys/class/misc/deepidle/idle_stats");
+ SysCommand.getInstance().readSysfs("/sys/class/misc/deepidle/idle_stats");
for(int i = 0; i < 3; ++i) {
String line = SysCommand.getInstance().getLastResult(i + 2);
Log.d(LOG_TAG, line);
@@ -145,7 +145,7 @@ private void showIdleStatsDialog() {
builder.setPositiveButton(getString(R.string.label_reset), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
- if(SysCommand.getInstance().suRun("echo", "1", ">", "/sys/class/misc/deepidle/reset_stats") < 0) {
+ if(SysCommand.getInstance().writeSysfs("/sys/class/misc/deepidle/reset_stats", "1") < 0) {
Log.d(LOG_TAG, "failed to reset deepidle stats");
}
dialog.dismiss();
@@ -89,7 +89,7 @@ private void reloadTweak() {
PreloadValues.getInstance().reload();
// save kernel version
- if(sc.suRun("cat", "/proc/version") > 0) {
+ if(sc.readSysfs("/proc/version") > 0) {
String kernel = sc.getLastResult(0);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
@@ -282,7 +282,7 @@ public static int loadSettingsOnBoot(Context c) {
// now check current kernel version with saved value
restore = false;
SysCommand sysCommand = SysCommand.getInstance();
- if(sysCommand.suRun("cat", "/proc/version") > 0) {
+ if(sysCommand.readSysfs("/proc/version") > 0) {
String kernel = sysCommand.getLastResult(0);
String savedKernelVersion = preferences.getString(c.getString(R.string.key_kernel_version), null);
if(kernel.equals(savedKernelVersion)) {
@@ -1,6 +1,9 @@
package mobi.cyann.nstools;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -16,6 +19,8 @@
private StreamGobbler out;
private StreamGobbler err;
+
+ private List<String> lastResult;
private final static SysCommand singleton;
@@ -27,6 +32,65 @@ public static SysCommand getInstance() {
return singleton;
}
+ private SysCommand() {
+ lastResult = new ArrayList<String>();
+ }
+
+ public int readSysfs(String sysfsPath) {
+ lastResult.clear();
+ File readThis = new File(sysfsPath);
+ if(readThis.canRead()) {
+ // read the file directly if we have read permission
+ BufferedReader br = null;
+ String line = null;
+ try {
+ br = new BufferedReader(new FileReader(readThis), 512);
+ do {
+ line = br.readLine();
+ if(line != null)
+ lastResult.add(line);
+ }while(line != null);
+ }catch (Exception e) {
+ Log.e(LOG_TAG, "Exception when reading " + sysfsPath, e);
+ }finally {
+ try {
+ br.close();
+ }catch(Exception e) {}
+ }
+ }
+ if(lastResult.size() == 0 && readThis.isFile()) { // the file is exists but we don't have permission
+ // so.. we need SU here
+ return suRun("cat", sysfsPath);
+ }else {
+ return lastResult.size();
+ }
+ }
+
+ public int writeSysfs(String sysfsPath, String value) {
+ int ret = -1;
+ File writeThis = new File(sysfsPath);
+ if(writeThis.canWrite()) {
+ // write the file directly if we have read permission
+ FileWriter fw = null;
+ try {
+ fw = new FileWriter(writeThis);
+ fw.write(value);
+ ret = 0;
+ }catch(Exception e) {
+ Log.e(LOG_TAG, "Exception when writing " + sysfsPath, e);
+ }finally {
+ try {
+ fw.close();
+ }catch(Exception e) {}
+ }
+ }
+ if(ret < 0) {
+ // so.. we need SU here
+ ret = suRun("echo", "\""+ value + "\"", ">", sysfsPath);
+ }
+ return ret;
+ }
+
/**
* run shell command with super user permission
*
@@ -70,6 +134,7 @@ public int suRun(String... cmd) {
result = -err.getLineCount();
} else {
result = out.getLineCount();
+ out.copyLines(lastResult);
}
} catch (Exception e) {
Log.e(LOG_TAG, "", e);
@@ -78,11 +143,6 @@ public int suRun(String... cmd) {
result = -1;
}
return result;
-
- /*
- return run("su", "-c", cmds.toString());
- */
-
}
/**
@@ -113,6 +173,7 @@ public int run(String... cmd) {
result = -err.getLineCount();
} else {
result = out.getLineCount();
+ out.copyLines(lastResult);
}
} catch (Exception e) {
Log.e(LOG_TAG, "", e);
@@ -130,19 +191,9 @@ public String getLastError(int line) {
return null;
}
- public void logLastError(String tag) {
- if(err != null)
- err.logLastLines(tag);
- }
-
- public void logLastOutput(String tag) {
- if(out != null)
- out.logLastLines(tag);
- }
-
public String getLastResult(int line) {
- if(out != null)
- return out.getLine(line);
+ if(lastResult.size() > line)
+ return lastResult.get(line);
else
return null;
}
@@ -193,20 +244,17 @@ public void waitFor() {
}
}
- public void logLastLines(String tag) {
- if(result != null) {
- for(int i = 0; i < result.size(); ++i) {
- Log.d(tag, result.get(i));
- }
- }
- }
-
public int getLineCount() {
return result.size();
}
public String getLine(int idx) {
return result.get(idx);
}
+
+ public void copyLines(List<String> copyTo) {
+ copyTo.clear();
+ copyTo.addAll(result);
+ }
}
}
@@ -109,7 +109,7 @@ private void createDefaultVoltPreference(PreferenceCategory parent, String voltP
private void readUvmvTable() {
PreferenceCategory c = (PreferenceCategory)findPreference(getString(R.string.key_arm_volt_pref));
SysCommand sc = SysCommand.getInstance();
- int count = sc.suRun("cat", "/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
+ int count = sc.readSysfs("/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
for(int i = 0; i < count; ++i) {
String line = sc.getLastResult(i);
String parts[] = line.split(":");
@@ -132,7 +132,7 @@ private void readVoltages(IntegerPreference maxVolt, String catKey, String voltP
}
SysCommand sc = SysCommand.getInstance();
- int count = sc.suRun("cat", voltDevice);
+ int count = sc.readSysfs(voltDevice);
for(int i = 0; i < count; ++i) {
String line = sc.getLastResult(i);
String parts[] = line.split(":");
@@ -155,7 +155,7 @@ private void saveVoltages(String key, List<Integer> voltageList, String deviceSt
}
Log.d(LOG_TAG, "voltages:" + s.toString());
if(deviceString != null) {
- SysCommand.getInstance().suRun("echo", "\""+s.toString()+"\"", ">", deviceString);
+ SysCommand.getInstance().writeSysfs(deviceString, "\""+s.toString()+"\"");
}
// save to xml pref
Editor ed = preferences.edit();
@@ -59,7 +59,7 @@ protected String readFromInterface() {
if(interfacePath != null) {
Log.d(LOG_TAG, "read from " + interfacePath);
SysCommand sc = SysCommand.getInstance();
- int n = sc.suRun("cat", interfacePath);
+ int n = sc.readSysfs(interfacePath);
if(n > 0) {
ret = sc.getLastResult(0);
Log.d(LOG_TAG, "ROK:" + ret);
@@ -76,7 +76,7 @@ protected void writeToInterface(String value) {
// echo to interfacePath
if(interfacePath != null) {
SysCommand sc = SysCommand.getInstance();
- if(sc.suRun("echo", value, ">", interfacePath) >= 0) {
+ if(sc.writeSysfs(interfacePath, value) >= 0) {
Log.d(LOG_TAG, "WOK");
}else {
Log.e(LOG_TAG, "WER:" + sc.getLastError(0));
@@ -64,7 +64,7 @@ protected void writeToInterface(String value) {
if(writeToInterfaces != null) {
SysCommand sc = SysCommand.getInstance();
for(CharSequence i: writeToInterfaces) {
- if(sc.suRun("echo", value, ">", i.toString()) >= 0) {
+ if(sc.writeSysfs(i.toString(), value) >= 0) {
Log.d(LOG_TAG, "WOK");
}else {
Log.e(LOG_TAG, "WER:" + sc.getLastError(0));

0 comments on commit 81a4a87

Please sign in to comment.