From bf8c2cb190461b77ad910caa2183f40517e16e36 Mon Sep 17 00:00:00 2001 From: secretrobotron Date: Thu, 14 Jul 2016 16:32:07 +0100 Subject: [PATCH] Improvements to Analytics gathering 1. Permissions in manifest to write to external storage. 2. Analytics in more/better places. 3. Write to external storage instead. --- app/src/main/AndroidManifest.xml | 1 + .../mozilla/hackathon/kiboko/Analytics.java | 39 ++++++++++++++----- .../kiboko/activities/DSOActivity.java | 7 ++-- .../kiboko/activities/ResultActivity.java | 6 ++- .../activities/TutorialSlideActivity.java | 7 ++++ .../kiboko/adapters/IconsAdapter.java | 2 + 6 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b650180..ad53dc8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/Analytics.java b/app/src/main/java/com/mozilla/hackathon/kiboko/Analytics.java index 14d95af..553188e 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/Analytics.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/Analytics.java @@ -1,9 +1,12 @@ package com.mozilla.hackathon.kiboko; import android.content.Context; +import android.os.Environment; +import android.util.Log; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -55,6 +58,15 @@ public class Analytics { 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() { save(true); } @@ -82,19 +94,26 @@ public class Analytics { // Replace this with save to disk functionality FileOutputStream outputStream; - try { - outputStream = App.getContext().openFileOutput(ANALYTICS_FILENAME, Context.MODE_PRIVATE | Context.MODE_APPEND); - - outputStream.write(output.getBytes()); - outputStream.close(); - mItems.clear(); - } - catch (Exception e) { - // Don't just consume a whole bunch of memory if something is going wrong. - if (mItems.size() > 100) { + if (isExternalStorageWritable()) { + try { + File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), ANALYTICS_FILENAME); + if (!outputFile.exists()) { + outputFile.createNewFile(); + } + outputStream = new FileOutputStream(outputFile, true); + outputStream.write(output.getBytes()); + outputStream.close(); 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) { diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/DSOActivity.java b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/DSOActivity.java index ad850e8..86f99b0 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/DSOActivity.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/DSOActivity.java @@ -2,13 +2,14 @@ package com.mozilla.hackathon.kiboko.activities; import android.support.v7.app.AppCompatActivity; +import com.mozilla.hackathon.kiboko.Analytics; import com.mozilla.hackathon.kiboko.App; import com.mozilla.hackathon.kiboko.events.ApplicationStateChanged; import com.mozilla.hackathon.kiboko.services.DataBootstrapService; 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 { @@ -22,7 +23,8 @@ public class DSOActivity extends AppCompatActivity { App.getBus().post(new ApplicationStateChanged(true)); super.onResume(); - System.out.println("Resume"); + + Analytics.add("Resumed DSO Activity", this.getClass().getSimpleName()); } @Override @@ -31,6 +33,5 @@ public class DSOActivity extends AppCompatActivity { App.getBus().post(new ApplicationStateChanged(false)); bus.unregister(this); super.onPause(); - System.out.println("Pause"); } } diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/ResultActivity.java b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/ResultActivity.java index dd2e4e0..6203fe8 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/ResultActivity.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/ResultActivity.java @@ -8,6 +8,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.TextView; +import com.mozilla.hackathon.kiboko.Analytics; import com.mozilla.hackathon.kiboko.R; import pl.droidsonroids.gif.GifImageView; @@ -34,9 +35,11 @@ public class ResultActivity extends AppCompatActivity { TextView txtPoints = (TextView) findViewById(R.id.quizResult); //get score Bundle b = getIntent().getExtras(); - int score= b.getInt("score"); + int score = b.getInt("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)); int randomIndex = new Double(Math.random() * imageResources.length).intValue(); gifImageView.setImageResource(imageResources[randomIndex]); @@ -54,6 +57,7 @@ public class ResultActivity extends AppCompatActivity { } public void playAgain(View view){ + Analytics.add("Icon Quiz Play Again"); Intent intent = new Intent(ResultActivity.this, IconQuizActivity.class); startActivity(intent); finish(); diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java index f4b96b8..f5500d1 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java @@ -21,6 +21,7 @@ import android.widget.TextView; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import com.mozilla.hackathon.kiboko.Analytics; import com.mozilla.hackathon.kiboko.R; import com.mozilla.hackathon.kiboko.fragments.ScreenSlidePageFragment; import com.mozilla.hackathon.kiboko.models.Step; @@ -94,6 +95,8 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager. mNext.setEnabled(true); 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); 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"); } } + + Analytics.add("Tutorial Slide", mTopic); } @Override diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/adapters/IconsAdapter.java b/app/src/main/java/com/mozilla/hackathon/kiboko/adapters/IconsAdapter.java index f87b635..7a2ecbc 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/adapters/IconsAdapter.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/adapters/IconsAdapter.java @@ -11,6 +11,7 @@ import android.widget.Filterable; import android.widget.ImageView; import android.widget.TextView; +import com.mozilla.hackathon.kiboko.Analytics; import com.mozilla.hackathon.kiboko.R; import com.mozilla.hackathon.kiboko.models.IconTopic; @@ -116,6 +117,7 @@ public class IconsAdapter extends BaseAdapter implements Filterable { // } // }); tooltip.show(); + Analytics.add("Icon List icon clicked", topic.getTag()); } });