Back to project page Pimp_my_Z1.
The source code is released under:
GNU General Public License
If you think the Android project Pimp_my_Z1 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** Copyright (C) 2013 Louis Teboul (a.k.a Androguide) *//from w ww . j a va2 s . c o m * admin@pimpmyrom.org || louisteboul@gmail.com * http://pimpmyrom.org || http://androguide.fr * 71 quai Clmenceau, 69300 Caluire-et-Cuire, FRANCE. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. **/ package com.androguide.honamicontrol.helpers; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.util.Log; import android.widget.Toast; import com.androguide.honamicontrol.helpers.CMDProcessor.CMDProcessor; import com.androguide.honamicontrol.helpers.CMDProcessor.CommandResult; import com.androguide.honamicontrol.kernel.cpucontrol.CPUInterface; import com.androguide.honamicontrol.kernel.gpucontrol.GPUInterface; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Date; public class CPUHelper { private static final String TAG = "CPUHelper"; private static BufferedReader br; /** * Checks device for SuperUser permission * * @return If SU was granted or denied */ public static boolean checkSu() { if (!new File("/system/bin/su").exists() && !new File("/system/xbin/su").exists()) { Log.e(TAG, "su does not exist!!!"); return false; // tell caller to bail... } try { new CMDProcessor(); CommandResult commandResult = CMDProcessor.runSuCommand("ls /data/app-private"); if (commandResult.success()) { Log.i(TAG, " SU exists and we have permission"); return true; } else { Log.i(TAG, " SU exists but we dont have permission"); return false; } } catch (final NullPointerException e) { Log.e(TAG, e.getLocalizedMessage().toString()); return false; } } /** * Checks device for network connectivity * * @return If the device has data connectivity */ public static boolean isNetworkAvailable(final Context c) { boolean state = false; if (c != null) { ConnectivityManager cm = (ConnectivityManager) c .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { Log.i(TAG, "The device currently has data connectivity"); state = true; } else { Log.i(TAG, "The device does not currently have data connectivity"); state = false; } } return state; } /** * Checks to see if Busybox is installed in "/system/" * * @return If busybox exists */ public static boolean checkBusybox() { if (!new File("/system/bin/busybox").exists() && !new File("/system/xbin/busybox").exists()) { Log.e(TAG, "Busybox not in xbin or bin!"); return false; } try { new CMDProcessor(); if (!CMDProcessor.runSuCommand("busybox mount").success()) { Log.e(TAG, " Busybox is there but it is borked! "); return false; } } catch (final NullPointerException e) { Log.e(TAG, e.getLocalizedMessage().toString()); return false; } return true; } public static String[] getMounts(final String path) { try { br = new BufferedReader(new FileReader("/proc/mounts"), 256); String line = null; while ((line = br.readLine()) != null) { if (line.contains(path)) { return line.split(" "); } } br.close(); } catch (FileNotFoundException e) { Log.d(TAG, "/proc/mounts does not exist"); } catch (IOException e) { Log.d(TAG, "Error reading /proc/mounts"); } return null; } public static boolean getMount(final String mount) { new CMDProcessor(); final String mounts[] = getMounts("/system"); if (mounts != null && mounts.length >= 3) { final String device = mounts[0]; final String path = mounts[1]; final String point = mounts[2]; if (CMDProcessor.runSuCommand( "mount -o " + mount + ",remount -t " + point + " " + device + " " + path) .success()) { return true; } } return (CMDProcessor.runSuCommand("busybox mount -o remount," + mount + " /system").success()); } public static String readOneLine(String fname) { BufferedReader br; String line = null; try { br = new BufferedReader(new FileReader(fname), 512); try { line = br.readLine(); } finally { br.close(); } } catch (Exception e) { Log.e(TAG, "IO Exception when reading sys file", e); // attempt to do magic! return readFileViaShell(fname, true); } return line; } public static String readOneLineNotRoot(String fname) { if (new File(fname).exists()) { BufferedReader br; String line = null; try { br = new BufferedReader(new FileReader(fname), 512); try { line = br.readLine(); } finally { br.close(); } } catch (Exception e) { Log.e(TAG, "IO Exception when reading sys file", e); // attempt to do magic! return readFileViaShell(fname, false); } return line; } else { return ""; } } public static String readFileViaShell(String filePath, boolean useSu) { CommandResult cr; if (useSu) { new CMDProcessor(); cr = CMDProcessor.runSuCommand("cat " + filePath); } else { new CMDProcessor(); cr = CMDProcessor.runShellCommand("cat " + filePath); } if (cr.success()) return cr.getStdout(); return null; } public static boolean writeOneLine(String fname, String value) { try { FileWriter fw = new FileWriter(fname); try { fw.write(value); } finally { fw.close(); } } catch (IOException e) { String Error = "Error writing to " + fname + ". Exception: "; Log.e(TAG, Error, e); return false; } return true; } public static ArrayList<String> getAvailableIOSchedulers() { ArrayList<String> schedulers = new ArrayList<String>(); String[] aux = readStringArray("/sys/block/mmcblk0/queue/scheduler"); if (aux != null) { for (int i = 0; i < aux.length; i++) { if (aux[i].charAt(0) == '[') { schedulers.add(i, aux[i].substring(1, aux[i].length() - 1)); } else { schedulers.add(i, aux[i]); } } } return schedulers; } public static ArrayList<String> getAvailableIOSchedulersSD() { ArrayList<String> schedulers = new ArrayList<String>(); String[] aux = readStringArray("/sys/block/mmcblk1/queue/scheduler"); if (aux != null) { for (int i = 0; i < aux.length; i++) { if (aux[i].charAt(0) == '[') { schedulers.add(i, aux[i].substring(1, aux[i].length() - 1)); } else { schedulers.add(i, aux[i]); } } } return schedulers; } public static String getIOScheduler() { String scheduler = null; String[] schedulers = readStringArray("/sys/block/mmcblk0/queue/scheduler"); if (schedulers != null) { for (String s : schedulers) { if (s.charAt(0) == '[') { scheduler = s.substring(1, s.length() - 1); break; } } } return scheduler; } public static String getIOSchedulerSD() { String scheduler = null; String[] schedulers = readStringArray("/sys/block/mmcblk1/queue/scheduler"); if (schedulers != null) { for (String s : schedulers) { if (s.charAt(0) == '[') { scheduler = s.substring(1, s.length() - 1); break; } } } return scheduler; } private static String[] readStringArray(String fname) { String line = readOneLine(fname); if (line != null) { return line.split(" "); } return null; } /** * Long toast message * * @param c Application Context * @param msg Message to send */ public static void msgLong(final Context c, final String msg) { if (c != null && msg != null) { Toast.makeText(c, msg.trim(), Toast.LENGTH_LONG).show(); } } /** * Short toast message * * @param c Application Context * @param msg Message to send */ public static void msgShort(final Context c, final String msg) { if (c != null && msg != null) { Toast.makeText(c, msg.trim(), Toast.LENGTH_SHORT).show(); } } /** * Long toast message * * @param c Application Context * @param msg Message to send */ public static void sendMsg(final Context c, final String msg) { if (c != null && msg != null) { msgLong(c, msg); } } /** * Return a timestamp * * @param context Application Context */ public static String getTimestamp(final Context context) { String timestamp; timestamp = "unknown"; Date now = new Date(); java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context); java.text.DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(context); if (dateFormat != null && timeFormat != null) { timestamp = dateFormat.format(now) + " " + timeFormat.format(now); } return timestamp; } public static boolean isPackageInstalled(final String packageName, final PackageManager pm) { String mVersion; try { mVersion = pm.getPackageInfo(packageName, 0).versionName; if (mVersion.equals(null)) { return false; } } catch (NameNotFoundException e) { return false; } return true; } public static void restartSystemUI() { new CMDProcessor(); CMDProcessor.runSuCommand("pkill -TERM -f com.android.systemui"); } public static void setSystemProp(String prop, String val) { new CMDProcessor(); CMDProcessor.runSuCommand("setprop " + prop + " " + val); } public static String getSystemProp(String prop, String def) { String result = getSystemProp(prop); return result == null ? def : result; } private static String getSystemProp(String prop) { new CMDProcessor(); CommandResult cr = CMDProcessor.runShellCommand("getprop " + prop); if (cr.success()) { return cr.getStdout(); } else { return null; } } public class asyncReadOneLine extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { String fname = params[0]; BufferedReader br; String line = null; try { br = new BufferedReader(new FileReader(fname), 512); try { line = br.readLine(); } finally { br.close(); } } catch (Exception e) { Log.e(TAG, "IO Exception when reading sys file", e); // attempt to do magic! return readFileViaShell(fname, false); } return line; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } public static String[] getAvailableCPUFreqs() { String[] availableFrequencies = new String[0]; String availableFrequenciesLine; if (Helpers.doesFileExist(CPUInterface.STEPS)) { availableFrequenciesLine = CPUHelper.readOneLineNotRoot(CPUInterface.STEPS); if (availableFrequenciesLine != null) { availableFrequencies = availableFrequenciesLine.split(" "); Arrays.sort(availableFrequencies, new Comparator<String>() { @Override public int compare(String object1, String object2) { return Integer.valueOf(object1).compareTo( Integer.valueOf(object2)); } }); } } return availableFrequencies; } public static String[] getAvailableGPUFreqs() { String[] availableFrequencies = new String[0]; String availableFrequenciesLine; if (Helpers.doesFileExist(GPUInterface.availableFreqs)) { availableFrequenciesLine = CPUHelper.readOneLineNotRoot(GPUInterface.availableFreqs); if (availableFrequenciesLine != null) { availableFrequencies = availableFrequenciesLine.split(" "); Arrays.sort(availableFrequencies, new Comparator<String>() { @Override public int compare(String object1, String object2) { return Integer.valueOf(object1).compareTo( Integer.valueOf(object2)); } }); } } return availableFrequencies; } }