1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DroidUptime/src/org/r3pek/droiduptime/DroidUptimeService.java

170 lines
5.5 KiB

package org.r3pek.droiduptime;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.app.Service;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
public class DroidUptimeService extends Service {
private Timer timer;
private ConfigValues cv;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
cv = new ConfigValues(getApplicationContext());
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
sendUptime();
}
}, 60000, cv.getUpdateInterval() * 1000);
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) timer.cancel();
}
@SuppressWarnings("static-access")
private String getMACAddrMD5() {
WifiManager manager = (WifiManager)getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
try {
return MD5.calculateMD5(info.getMacAddress().toLowerCase().replaceAll(":", ""));
} catch (Exception e) { }
return "";
}
private String getFormattedKernelVersion() {
String procVersionStr;
try {
BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
try {
procVersionStr = reader.readLine();
} finally {
reader.close();
}
final String PROC_VERSION_REGEX = "\\w+\\s+" + /* ignore: Linux */
"\\w+\\s+" + /* ignore: version */
"([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
"\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
"\\([^)]+\\)\\s+" + /* ignore: (gcc ..) */
"([^\\s]+)\\s+" + /* group 3: #26 */
"(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
"(.+)"; /* group 4: date */
Pattern p = Pattern.compile(PROC_VERSION_REGEX);
Matcher m = p.matcher(procVersionStr);
if (!m.matches())
return "Unavailable";
else
return m.group(1);
} catch (IOException e) {
return "Unavailable";
}
}
private String getCPUABI() {
String procStr;
try {
BufferedReader reader = new BufferedReader(new FileReader("/proc/cpuinfo"), 256);
try {
procStr = reader.readLine();
} finally {
reader.close();
}
String[] cpu = procStr.replace("(", "").replace(")", "").split(" ");
return cpu[1] + "-" + cpu[5];
} catch (Exception e) {
return "";
}
}
@SuppressWarnings("static-access")
public void sendUptime() {
if (cv.getUsername().equals("") || cv.getPassword().equals(""))
return;
ConnectivityManager conMngr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
boolean isConnected = false || conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
isConnected |= conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
if (!isConnected) return;
long uptime = SystemClock.elapsedRealtime() / 1000;
String agent = "DroidUptime-v1.3";
String cnb = "1";
String mac = getMACAddrMD5();
String hostname = cv.getHostname();
String machine = hostname.equals("") ? android.os.Build.MODEL : hostname + " (" + android.os.Build.MODEL + ")";
String os = "Linux+" + getFormattedKernelVersion() + "+(" + getCPUABI() + ")";
String distrib = "Android";
String distribContent = "SDK " + android.os.Build.VERSION.RELEASE;
distribContent = Base64.encodeToString(distribContent.getBytes(), false);
Log.d("DroidUptime", agent);
Log.d("DroidUptime", cnb);
Log.d("DroidUptime", mac);
Log.d("DroidUptime", machine);
Log.d("DroidUptime", os);
Log.d("DroidUptime", distrib);
Log.d("DroidUptime", distribContent);
Log.d("DroidUptime", cv.getUsername());
Log.d("DroidUptime", cv.getPassword());
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://update.uptimeprj.com/update.php?username=" + cv.getUsername());
post.addHeader("User-Agent", agent);
List <NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("username", cv.getUsername()));
data.add(new BasicNameValuePair("pass", cv.getPassword()));
data.add(new BasicNameValuePair("cnb", cnb));
data.add(new BasicNameValuePair("mac", mac));
data.add(new BasicNameValuePair("machine", machine));
data.add(new BasicNameValuePair("os", os));
data.add(new BasicNameValuePair("uptime", String.valueOf(uptime)));
data.add(new BasicNameValuePair("distrib", distrib));
data.add(new BasicNameValuePair("distribcontent", distribContent));
post.setEntity(new UrlEncodedFormEntity(data, HTTP.UTF_8));
httpclient.execute(post);
cv.setLastUpdate(System.currentTimeMillis());
} catch (Exception e) { e.printStackTrace(); }
}
}