Merge pull request #159 from secretrobotron/t87

Improvements to Analytics gathering
This commit is contained in:
Brian Mwadime 2016-07-14 20:01:45 +03:00 коммит произвёл GitHub
Родитель 8592ab06b7 35c78d9710
Коммит 8693bd1e0e
6 изменённых файлов: 72 добавлений и 14 удалений

Просмотреть файл

@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

Просмотреть файл

@ -1,9 +1,15 @@
package com.mozilla.hackathon.kiboko; package com.mozilla.hackathon.kiboko;
import android.content.Context; import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -15,7 +21,9 @@ import java.util.List;
public class Analytics { public class Analytics {
private static final String ANALYTICS_FILENAME = "jisort_analytics.txt"; private static final String ANALYTICS_FILENAME = "jisort_analytics.txt";
private static final String ANALYTICS_ARCHIVE_FILENAME = "jisort_analytics.1.txt";
private static final long TIME_BETWEEN_SAVES= 5000; private static final long TIME_BETWEEN_SAVES= 5000;
private static final long FILE_SIZE_LIMIT = 100000; //bytes
private class AnalyticsItem { private class AnalyticsItem {
String mName; String mName;
@ -55,10 +63,33 @@ public class Analytics {
mLastSaveTime = System.currentTimeMillis(); mLastSaveTime = System.currentTimeMillis();
} }
/* Checks if external storage is available for read and write */
private boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
private void flushItems() { private void flushItems() {
save(true); save(true);
} }
private void copyOldAnalytics() throws IOException {
InputStream in = new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ANALYTICS_FILENAME));
OutputStream out = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ANALYTICS_ARCHIVE_FILENAME));
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
private void save() { private void save() {
save(false); save(false);
} }
@ -82,19 +113,31 @@ public class Analytics {
// Replace this with save to disk functionality // Replace this with save to disk functionality
FileOutputStream outputStream; FileOutputStream outputStream;
try { if (isExternalStorageWritable()) {
outputStream = App.getContext().openFileOutput(ANALYTICS_FILENAME, Context.MODE_PRIVATE | Context.MODE_APPEND); try {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ANALYTICS_FILENAME);
outputStream.write(output.getBytes()); if (!outputFile.exists()) {
outputStream.close(); outputFile.createNewFile();
mItems.clear(); }
} else if (outputFile.length() > FILE_SIZE_LIMIT){
catch (Exception e) { copyOldAnalytics();
// Don't just consume a whole bunch of memory if something is going wrong. outputFile.delete();
if (mItems.size() > 100) { outputFile.createNewFile();
}
outputStream = new FileOutputStream(outputFile, true);
outputStream.write(output.getBytes());
outputStream.close();
mItems.clear(); mItems.clear();
} catch (Exception e) {
// Don't just consume a whole bunch of memory if something is going wrong.
if (mItems.size() > 100) {
mItems.clear();
}
} }
} }
else {
mItems.clear();
}
} }
public void addItem(String name, String data) { public void addItem(String name, String data) {

Просмотреть файл

@ -2,13 +2,14 @@ package com.mozilla.hackathon.kiboko.activities;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import com.mozilla.hackathon.kiboko.Analytics;
import com.mozilla.hackathon.kiboko.App; import com.mozilla.hackathon.kiboko.App;
import com.mozilla.hackathon.kiboko.events.ApplicationStateChanged; import com.mozilla.hackathon.kiboko.events.ApplicationStateChanged;
import com.mozilla.hackathon.kiboko.services.DataBootstrapService; import com.mozilla.hackathon.kiboko.services.DataBootstrapService;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
/** /**
* Created by Brian Mwadime on 06/06/2016. * Created by secretrobotron in July of 2016.
*/ */
public class DSOActivity extends AppCompatActivity { public class DSOActivity extends AppCompatActivity {
@ -22,7 +23,8 @@ public class DSOActivity extends AppCompatActivity {
App.getBus().post(new ApplicationStateChanged(true)); App.getBus().post(new ApplicationStateChanged(true));
super.onResume(); super.onResume();
System.out.println("Resume");
Analytics.add("Resumed DSO Activity", this.getClass().getSimpleName());
} }
@Override @Override
@ -31,6 +33,5 @@ public class DSOActivity extends AppCompatActivity {
App.getBus().post(new ApplicationStateChanged(false)); App.getBus().post(new ApplicationStateChanged(false));
bus.unregister(this); bus.unregister(this);
super.onPause(); super.onPause();
System.out.println("Pause");
} }
} }

Просмотреть файл

@ -8,6 +8,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.mozilla.hackathon.kiboko.Analytics;
import com.mozilla.hackathon.kiboko.R; import com.mozilla.hackathon.kiboko.R;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
@ -34,9 +35,11 @@ public class ResultActivity extends AppCompatActivity {
TextView txtPoints = (TextView) findViewById(R.id.quizResult); TextView txtPoints = (TextView) findViewById(R.id.quizResult);
//get score //get score
Bundle b = getIntent().getExtras(); Bundle b = getIntent().getExtras();
int score= b.getInt("score"); int score = b.getInt("score");
txtPoints.setText(getString(R.string.quiz_template_points, score)); txtPoints.setText(getString(R.string.quiz_template_points, score));
Analytics.add("Icon Quiz Finished", new Integer(score).toString());
GifImageView gifImageView = (GifImageView) findViewById((R.id.result_image)); GifImageView gifImageView = (GifImageView) findViewById((R.id.result_image));
int randomIndex = new Double(Math.random() * imageResources.length).intValue(); int randomIndex = new Double(Math.random() * imageResources.length).intValue();
gifImageView.setImageResource(imageResources[randomIndex]); gifImageView.setImageResource(imageResources[randomIndex]);
@ -54,6 +57,7 @@ public class ResultActivity extends AppCompatActivity {
} }
public void playAgain(View view){ public void playAgain(View view){
Analytics.add("Icon Quiz Play Again");
Intent intent = new Intent(ResultActivity.this, IconQuizActivity.class); Intent intent = new Intent(ResultActivity.this, IconQuizActivity.class);
startActivity(intent); startActivity(intent);
finish(); finish();

Просмотреть файл

@ -21,6 +21,7 @@ import android.widget.TextView;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.mozilla.hackathon.kiboko.Analytics;
import com.mozilla.hackathon.kiboko.R; import com.mozilla.hackathon.kiboko.R;
import com.mozilla.hackathon.kiboko.fragments.ScreenSlidePageFragment; import com.mozilla.hackathon.kiboko.fragments.ScreenSlidePageFragment;
import com.mozilla.hackathon.kiboko.models.Step; import com.mozilla.hackathon.kiboko.models.Step;
@ -94,6 +95,8 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager.
mNext.setEnabled(true); mNext.setEnabled(true);
mPrev.setEnabled(false); mPrev.setEnabled(false);
} }
Analytics.add("Tutorial Slide", mTopic + ", " + new Integer(mPager.getCurrentItem()).toString());
} }
}); });
@ -107,6 +110,8 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager.
mNext.setEnabled(false); mNext.setEnabled(false);
mPrev.setEnabled(true); mPrev.setEnabled(true);
} }
Analytics.add("Tutorial Slide", mTopic + ", " + new Integer(mPager.getCurrentItem()).toString());
} }
}); });
@ -158,6 +163,8 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager.
mTopic = (String)intent.getExtras().get("topic"); mTopic = (String)intent.getExtras().get("topic");
} }
} }
Analytics.add("Tutorial Slide", mTopic);
} }
@Override @Override

Просмотреть файл

@ -11,6 +11,7 @@ import android.widget.Filterable;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.mozilla.hackathon.kiboko.Analytics;
import com.mozilla.hackathon.kiboko.R; import com.mozilla.hackathon.kiboko.R;
import com.mozilla.hackathon.kiboko.models.IconTopic; import com.mozilla.hackathon.kiboko.models.IconTopic;
@ -116,6 +117,7 @@ public class IconsAdapter extends BaseAdapter implements Filterable {
// } // }
// }); // });
tooltip.show(); tooltip.show();
Analytics.add("Icon List icon clicked", topic.getTag());
} }
}); });