Android Open Source - droidling Personal Stats






From Project

Back to project page droidling.

License

The source code is released under:

Copyright (c) 2012 Keith Trnka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...

If you think the Android project droidling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.ktrnka.droidling;
/* w w  w .j av  a  2  s  .  c om*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;

/**
 * Wrapper for the output of PersonalActivity to load/save.
 */
public class PersonalStats {
    public static final int PHRASE_SORTED = 0;
    public static final int COUNT_SORTED = 1;
    private static final String TAG = "PersonalStats";

    StringBuilder[] keyPhraseTexts;
    StringBuilder contactsDisplay;
    StringBuilder generalDisplay;

    int[] dayHistogram;
    int[] hourHistogram;

    public PersonalStats() {
        keyPhraseTexts = new StringBuilder[2];
        for (int i = 0; i < keyPhraseTexts.length; i++)
            keyPhraseTexts[i] = new StringBuilder();

        contactsDisplay = new StringBuilder();
        generalDisplay = new StringBuilder();

        dayHistogram = new int[7];
        for (int i = 0; i < dayHistogram.length; i++)
            dayHistogram[i] = 0;

        hourHistogram = new int[25];
        for (int i = 0; i < hourHistogram.length; i++)
            hourHistogram[i] = 0;
    }

    public PersonalStats(FileInputStream in) throws IOException, JSONException {
        this();
        readFrom(in);
    }

    private void readFrom(FileInputStream in) throws IOException, JSONException {
        long previousTime = System.currentTimeMillis();
        
        // read file into a string
        StringBuilder b = new StringBuilder();
        BufferedReader charIn = new BufferedReader(new InputStreamReader(in));
        String line;
        while ( (line = charIn.readLine()) != null) {
            b.append(line);
        }
        charIn.close();
        
        // parse it
        JSONTokener tok = new JSONTokener(b.toString());
        JSONObject json = (JSONObject) tok.nextValue();
        keyPhraseTexts[PHRASE_SORTED].append(json.getString("phrase_sorted"));
        keyPhraseTexts[COUNT_SORTED].append(json.getString("count_sorted"));
        contactsDisplay.append(json.getString("contacts"));
        generalDisplay.append(json.getString("general"));
        
        JSONArray dayJson = json.getJSONArray("by_day");
        for (int i = 0; i < dayHistogram.length && i < dayJson.length(); i++)
            dayHistogram[i] = dayJson.getInt(i);
        
        JSONArray hourJson = json.getJSONArray("by_hour");
        for (int i = 0; i < hourHistogram.length && i < hourJson.length(); i++)
            hourHistogram[i] = hourJson.getInt(i);
        
        Log.i(TAG, "Read data in " + (System.currentTimeMillis() - previousTime) + " ms");
    }

    public void writeTo(FileOutputStream out) throws IOException, JSONException {
        long previousTime = System.currentTimeMillis();
        
        JSONObject json = new JSONObject();
        json.put("phrase_sorted", keyPhraseTexts[PHRASE_SORTED].toString());
        json.put("count_sorted", keyPhraseTexts[COUNT_SORTED].toString());
        json.put("contacts", contactsDisplay.toString());
        json.put("general", generalDisplay.toString());
        
        JSONArray dayJson = new JSONArray();
        for (int i = 0; i < dayHistogram.length; i++)
            dayJson.put(i, dayHistogram[i]);
        json.put("by_day", dayJson);
        
        JSONArray hourJson = new JSONArray();
        for (int i = 0; i < hourHistogram.length; i++)
            hourJson.put(i, hourHistogram[i]);
        json.put("by_hour", hourJson);
        
        BufferedWriter charOut = new BufferedWriter(new OutputStreamWriter(out));
        charOut.write(json.toString());
        charOut.close();

        Log.i(TAG, "Wrote data in " + (System.currentTimeMillis() - previousTime) + " ms");
    }
}




Java Source Code List

com.github.ktrnka.droidling.AboutActivity.java
com.github.ktrnka.droidling.AboutInterpersonalActivity.java
com.github.ktrnka.droidling.AboutLangIDActivity.java
com.github.ktrnka.droidling.AboutPersonalActivity.java
com.github.ktrnka.droidling.CorpusStats.java
com.github.ktrnka.droidling.DateDistribution.java
com.github.ktrnka.droidling.DiagnosticActivity.java
com.github.ktrnka.droidling.ExtendedApplication.java
com.github.ktrnka.droidling.GraphCard.java
com.github.ktrnka.droidling.ImageAdapter.java
com.github.ktrnka.droidling.InterpersonalActivity.java
com.github.ktrnka.droidling.InterpersonalCard.java
com.github.ktrnka.droidling.InterpersonalSingleStats.java
com.github.ktrnka.droidling.InterpersonalStats.java
com.github.ktrnka.droidling.LIDStats.java
com.github.ktrnka.droidling.LanguageIdentificationActivity.java
com.github.ktrnka.droidling.LanguageIdentifier.java
com.github.ktrnka.droidling.MainActivity.java
com.github.ktrnka.droidling.PersonalActivity.java
com.github.ktrnka.droidling.PersonalStats.java
com.github.ktrnka.droidling.RefreshableActivity.java
com.github.ktrnka.droidling.ShareableCard.java
com.github.ktrnka.droidling.Sms.java
com.github.ktrnka.droidling.Tokenizer.java
com.github.ktrnka.droidling.WordDistribution.java
com.github.ktrnka.droidling.helpers.AsyncDrawable.java
com.github.ktrnka.droidling.helpers.BitmapLoaderTask.java
com.github.ktrnka.droidling.helpers.Util.java