blob: 8b8549ab3d3a17ef8e6396d5578ba14654e17252 [file] [log] [blame]
/*
* Copyright (C) 2016 Fairphone B.V.
*
* 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 com.fairphone.hiccup.app;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import com.fairphone.hiccup.HiccupdService;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Process;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class HiccupUtil {
private static final String FILENAME_PROC_CPUINFO = "/proc/cpuinfo";
static void LOG(String msg) {
Log.i("HICCUP", msg);
}
public static String getBuildFingerPrint() {
Process proc = null;
try {
proc = Runtime.getRuntime().exec(new String[]{"/system/bin/getprop", "ro.build.fingerprint"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String getUptime() {
Process proc = null;
try {
proc = Runtime.getRuntime().exec(new String[]{"/system/bin/uptime"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String getBootReason() {
String ret = null;
try {
HiccupdService hiccupd = HiccupdService.getInstance();
ret = hiccupd.getBootReason();
LOG("BOOTREASON: " + ret);
} catch (Exception e) {
HiccupUtil.LOG("error talking to Hiccupd\n" + e.toString());
}
return ret;
}
public static boolean isHiccupdRunning() {
return HiccupdService.getInstance() != null;
}
public static String getPowerOnReason() {
String ret = null;
try {
HiccupdService hiccupd = HiccupdService.getInstance();
ret = hiccupd.getPowerOnReason();
LOG("PON_REASON: " + ret);
} catch (Exception e) {
HiccupUtil.LOG("error talking to Hiccupd\n" + e.toString());
}
return ret;
}
public static String getPowerOffReason() {
String ret = null;
try {
HiccupdService hiccupd = HiccupdService.getInstance();
ret = hiccupd.getPowerOffReason();
LOG("POFF_REASON: " + ret);
} catch (Exception e) {
HiccupUtil.LOG("error talking to Hiccupd\n" + e.toString());
}
return ret;
}
public static int getVersionCode(Context ctx) {
PackageInfo pInfo = null;
try {
pInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
return pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}
static boolean isUserDebugBuild() {
return getBuildFingerPrint().contains("userdebug");
}
static public Date getBoardDate() {
return BoardDate.getBoardDate();
}
/**
* Returns the Hardware value in /proc/cpuinfo, else returns "Unknown".
* @return a string that describes the processor
*/
public static String getDeviceProcessorInfo() {
// Hardware : XYZ
final String PROC_HARDWARE_REGEX = "Hardware\\s*:\\s*(.*)$"; /* hardware string */
try {
BufferedReader reader = new BufferedReader(new FileReader(FILENAME_PROC_CPUINFO));
String cpuinfo;
try {
while (null != (cpuinfo = reader.readLine())) {
if (cpuinfo.startsWith("Hardware")) {
Matcher m = Pattern.compile(PROC_HARDWARE_REGEX).matcher(cpuinfo);
if (m.matches()) {
return m.group(1);
}
}
}
return "Unknown";
} finally {
reader.close();
}
} catch (IOException e) {
LOG("IO Exception when getting cpuinfo for Device Info screen");
return "Unknown";
}
}
public static boolean isOwner(Context ctx)
{
UserHandle userHandle = android.os.Process.myUserHandle();
UserManager userManager = (UserManager) ctx.getSystemService(Context.USER_SERVICE);
if(null != userManager)
{
long userSerialNumber = userManager.getSerialNumberForUser(userHandle);
return 0 == userSerialNumber;
}
else
return false;
}
}