CurrentInfo.java 4.4 KB
Newer Older
A
commit  
andrewleo 已提交
1 2
package com.netease.qa.emmagee.utils;

A
andrewleo 已提交
3 4 5 6 7
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
8
import java.util.Locale;
A
andrewleo 已提交
9 10 11 12

import android.os.Build;
import android.util.Log;

A
andrewleo 已提交
13 14 15 16 17 18
/**
 * Current info
 * 
 * @author andrewleo
 * 
 */
A
commit  
andrewleo 已提交
19
public class CurrentInfo {
A
andrewleo 已提交
20
	private static final String LOG_TAG = "Emmagee-CurrentInfo";
21 22 23 24 25 26 27
	private static final String BUILD_MODEL = Build.MODEL.toLowerCase(Locale.ENGLISH);
	private static final String I_MBAT = "I_MBAT: ";
	private static final String CURRENT_NOW = "/sys/class/power_supply/battery/current_now";
	private static final String BATT_CURRENT = "/sys/class/power_supply/battery/batt_current";
	private static final String SMEM_TEXT = "/sys/class/power_supply/battery/smem_text";
	private static final String BATT_CURRENT_ADC = "/sys/class/power_supply/battery/batt_current_adc";
	private static final String CURRENT_AVG = "/sys/class/power_supply/battery/current_avg";
A
andrewleo 已提交
28

A
andrewleo 已提交
29 30 31 32 33
	/**
	 * read system file to get current value
	 * 
	 * @return current value
	 */
34
	public Long getCurrentValue() {
A
andrewleo 已提交
35
		File f = null;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		Log.d(LOG_TAG, BUILD_MODEL);
		// galaxy s4,oppo find,samgsung note2
		if (BUILD_MODEL.contains("sgh-i337") || BUILD_MODEL.contains("gt-i9505") || BUILD_MODEL.contains("sch-i545")
				|| BUILD_MODEL.contains("find 5") || BUILD_MODEL.contains("sgh-m919") || BUILD_MODEL.contains("sgh-i537")
				|| BUILD_MODEL.contains("x907") || BUILD_MODEL.contains("gt-n7100")) {
			f = new File(CURRENT_NOW);
			if (f.exists()) {
				return getCurrentValue(f, false);
			}
		}

		// samsung galaxy
		if (BUILD_MODEL.contains("gt-p31") || BUILD_MODEL.contains("gt-p51")) {
			f = new File(CURRENT_AVG);
			if (f.exists()) {
				return getCurrentValue(f, false);
			}
		}

		// htc desire hd ,desire z
		if (BUILD_MODEL.contains("desire hd") || BUILD_MODEL.contains("desire z")) {
			f = new File(BATT_CURRENT);
A
andrewleo 已提交
58 59 60 61
			if (f.exists())
				return getCurrentValue(f, false);
		}

62 63
		// htc sensation z710e
		f = new File(BATT_CURRENT);
A
andrewleo 已提交
64 65
		if (f.exists())
			return getCurrentValue(f, false);
66 67 68

		// htc one V
		f = new File(SMEM_TEXT);
A
andrewleo 已提交
69 70
		if (f.exists())
			return getSMemValue();
71 72 73

		// nexus one,meizu
		f = new File(CURRENT_NOW);
A
andrewleo 已提交
74 75
		if (f.exists())
			return getCurrentValue(f, true);
76

dezng's avatar
dezng 已提交
77 78 79 80 81
		// meizu pro 5
		f = new File("/sys/class/power_supply/bq2753x-0/current_now");
		if (f.exists())
			return getCurrentValue(f, true);

82 83
		// galaxy note, galaxy s2
		f = new File(BATT_CURRENT_ADC);
A
andrewleo 已提交
84 85
		if (f.exists())
			return getCurrentValue(f, false);
86 87 88

		// acer V360
		f = new File("/sys/class/power_supply/battery/BatteryAverageCurrent");
A
andrewleo 已提交
89 90
		if (f.exists())
			return getCurrentValue(f, false);
91 92 93 94 95 96

		// moto milestone,moto mb526
		f = new File("/sys/devices/platform/cpcap_battery/power_supply/usb/current_now");
		if (f.exists())
			return getCurrentValue(f, false);

A
andrewleo 已提交
97 98 99
		return null;
	}

100
	/**
A
andrewleo 已提交
101
	 * get current value from smem_text
102
	 * 
A
andrewleo 已提交
103
	 * @return current value
104 105
	 */
	public Long getSMemValue() {
A
andrewleo 已提交
106 107
		boolean success = false;
		String text = null;
108
		Long value = null;
A
andrewleo 已提交
109
		try {
110
			FileReader fr = new FileReader(SMEM_TEXT);
A
andrewleo 已提交
111 112 113
			BufferedReader br = new BufferedReader(fr);
			String line = br.readLine();
			while (line != null) {
114 115
				if (line.contains(I_MBAT)) {
					text = line.substring(line.indexOf(I_MBAT) + 8);
A
andrewleo 已提交
116 117 118 119 120 121
					success = true;
					break;
				}
				line = br.readLine();
			}
			fr.close();
122 123 124
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
A
andrewleo 已提交
125 126 127 128 129
		}
		if (success) {
			try {
				value = Long.parseLong(text);
			} catch (NumberFormatException nfe) {
130
				nfe.printStackTrace();
A
andrewleo 已提交
131 132 133 134 135 136
				value = null;
			}
		}
		return value;
	}

A
andrewleo 已提交
137
	/**
A
andrewleo 已提交
138
	 * read system file to get current value
A
andrewleo 已提交
139 140
	 * 
	 * @param file
A
andrewleo 已提交
141 142
	 * @param convertToMillis 
	 * @return current value
A
andrewleo 已提交
143
	 */
A
andrewleo 已提交
144
	public Long getCurrentValue(File file, boolean convertToMillis) {
145 146 147 148 149 150
		Log.d(LOG_TAG, "*** getCurrentValue ***");
		Log.d(LOG_TAG, "*** " + convertToMillis + " ***");
		String line = null;
		Long value = null;
		FileInputStream fs = null;
		DataInputStream ds = null;
A
andrewleo 已提交
151
		try {
152 153 154 155 156 157 158 159 160 161 162 163
			fs = new FileInputStream(file);
			ds = new DataInputStream(fs);
			line = ds.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fs.close();
				ds.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
A
andrewleo 已提交
164
		}
165
		if (line != null) {
A
andrewleo 已提交
166
			try {
167
				value = Long.parseLong(line);
A
andrewleo 已提交
168 169 170 171
			} catch (NumberFormatException nfe) {
				value = null;
			}
			if (convertToMillis)
172
				value = value / 1000;
A
andrewleo 已提交
173 174 175
		}
		return value;
	}
A
commit  
andrewleo 已提交
176
}