package com.agilefreaks.calin.securityflaxexample;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/*
Things to consider:
- clarity with respect to security (we want to look at a abstraction and the security model should be very clear)
- catch flaws at compile time
- proper usage to be obvious
- separate and protect security state
 */

/*
Build abstractions that are hard to use insecurely
 */

public class StorageStrategy {
  private interface StorageHandler {
    public void store(String key, String data);
  }

  private class PublicStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on sdcard");
    }
  }

  private class PrivateStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on private folder");
    }
  }

  private Map<String, StorageHandler> handlerMapping = new HashMap<String, StorageHandler>();

  public StorageStrategy() {
    handlerMapping.put("private", new PrivateStoreAdapter());
    handlerMapping.put("public", new PublicStoreAdapter());
  }

  public void storeData(String key, String data, Boolean isPublic) {
    StorageHandler storageHandler;

    if (isPublic) {
      storageHandler = getPublicHandler();
    }
    else {
      storageHandler = getSecureHandler();
    }

    storageHandler.store(key, data);
  }

  private StorageHandler getPublicHandler() {
    return handlerMapping.get("public");
  }

  private StorageHandler getSecureHandler() {
    return handlerMapping.get("private");
  }

  public void saveSettings() {
    storeData("name", "private stuff", true);
    storeData("private.groups", "some more private stuff", true);
    storeData("address", "private address", true);

    storeData("profilephoto", "public info", false);
    storeData("homepageurl", "public info", false);
  }
}

@@ -418,16 +418,35 @@
 ly%0A */%0A%0A
+/*%0AAdd a enum%0A */%0A%0A
 public c
@@ -468,16 +468,52 @@
 ategy %7B%0A
+  public enum Security %7BMAX, NONE%7D%0A%0A
   privat
@@ -1291,24 +1291,25 @@
 ta, 
-Boolean isPublic
+Security security
 ) %7B%0A
@@ -1356,16 +1356,33 @@
 if (
-isPublic
+security == Security.NONE
 ) %7B%0A
@@ -1429,20 +1429,16 @@
 );%0A    %7D
-%0A   
  else %7B%0A
@@ -1776,36 +1776,44 @@
 private stuff%22, 
-true
+Security.MAX
 );%0A    storeData
@@ -1858,20 +1858,28 @@
 stuff%22, 
-true
+Security.MAX
 );%0A    s
@@ -1921,12 +1921,20 @@
 s%22, 
-true
+Security.MAX
 );%0A%0A
@@ -1974,29 +1974,37 @@
 blic info%22, 
-false
+Security.NONE
 );%0A    store
@@ -2042,13 +2042,21 @@
 o%22, 
-false
+Security.NONE
 );%0A 

Make the abstraction even more clear