Back to project page pinpoint-android.
The source code is released under:
MIT License
If you think the Android project pinpoint-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package co.islovely.pinpoint; /* w w w .j av a2 s. c o m*/ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.ActivityNotFoundException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Looper; import android.util.DisplayMetrics; import android.view.View; import android.widget.ListView; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class StatisticsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.statistics); /* TaskLog taskLog = Pinpoint.getUser().getTaskLog(); */ /* ((TextView) this.findViewById(R.id.correct_entries_count)).setText( */ /* "Correct: " + taskLog.getCorrectTaskLogEntriesCount() */ /* ); */ /* ((TextView) this.findViewById(R.id.entries_count)).setText( */ /* "Total: " + taskLog.getTaskLogEntriesCount() */ /* ); */ /* ((TextView) this.findViewById(R.id.success_quota)).setText( */ /* "Success quota: " + taskLog.getSuccessQuota() * 100 + "%" */ /* ); */ /* ((TextView) this.findViewById(R.id.average_duration)).setText( */ /* "Average duration: " + taskLog.getAverageDuration() + "ms" */ /* ); */ } public void sendLogs(View view) { String prettyJSON = ""; JSONObject jsonObject = new JSONObject(); JSONArray launcheritems = new JSONArray(), participants = new JSONArray(); JSONObject device = new JSONObject(), screen = new JSONObject(); DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); DeviceMetrics deviceMetrics = new DeviceMetrics(view.getContext().getPackageManager(), displayMetrics); try { jsonObject.put("device", deviceMetrics.toJSON()); jsonObject.put("configuration", Configuration.getInstance().toJSON()); for (LauncherItem launcheritem : LauncherReader.readDatabase(this)) { launcheritems.put(launcheritem.toJSON()); } jsonObject.put("launcheritems", launcheritems); participants.put(Pinpoint.getUser().toJSONObject()); participants.put(Pinpoint.getAttacker().toJSONObject()); jsonObject.put("participants", participants); } catch (JSONException exception) { Pinpoint.log("Could not create JSONObject."); } try { prettyJSON = jsonObject.toString(2); } catch (JSONException exception) { Pinpoint.log("Could not generate pretty JSON."); } ((TextView) this.findViewById(R.id.logs)).setText( prettyJSON ); this.sendLogsViaAPI(jsonObject); this.sendLogsViaMail(prettyJSON); } private void sendLogsViaAPI(final JSONObject jsonObject) { new Thread(new Runnable() { @Override public void run() { BufferedReader bufferedReader; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://pinpoint.islovely.co/api/v1/entries"); HttpResponse response = null; InputStream inputStream = null; String line; StringBuilder stringBuilder = new StringBuilder(); post.setHeader("Content-Type", "application/json"); try { post.setEntity(new StringEntity(jsonObject.toString())); } catch (UnsupportedEncodingException exception) { Pinpoint.log("Encoding not supported."); } try { response = client.execute(post); } catch (IOException exception) { Pinpoint.log("Could not execute POST-request."); } if (null != response) { try { inputStream = response.getEntity().getContent(); } catch (IOException exception) { Pinpoint.log("Could not read response."); } if (null != inputStream) { bufferedReader = new BufferedReader( new InputStreamReader(inputStream) ); try { while (null != (line = bufferedReader.readLine())) { stringBuilder.append(line); } } catch (IOException exception) { Pinpoint.log("Could not read line from response."); } Pinpoint.log("Response: " + stringBuilder.toString()); } } } }).start(); } private void sendLogsViaMail(String body) { FileOutputStream outputStream; Intent mailIntent = new Intent(Intent.ACTION_SEND); String attachmentName = "log.txt", recipient = "dominik.habersack@campus.lmu.de", subject = "[LOGFILE]"; mailIntent.setType("message/rfc822"); mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{ recipient }); mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); // write body to internal storage and attach file try { outputStream = this.openFileOutput(attachmentName, Context.MODE_WORLD_READABLE); outputStream.write(body.getBytes()); outputStream.close(); mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File(getFilesDir().toString(), attachmentName)) ); } catch (Exception e) { e.printStackTrace(); } try { this.startActivity(Intent.createChooser(mailIntent, "Send logs to " + recipient)); } catch (ActivityNotFoundException e) { Pinpoint.log("No email clients found."); } } }