Skip to content

Commit

Permalink
Merge pull request #7 from ama-csail/demo-v1
Browse files Browse the repository at this point in the history
Demo v1
  • Loading branch information
vontell committed Mar 12, 2017
2 parents 3e019c0 + 9ae0372 commit be63c41
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 108 deletions.
76 changes: 63 additions & 13 deletions ama/src/main/java/edu/mit/dig/ama/core/AMA.java
Expand Up @@ -11,13 +11,18 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.IntegerRes;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
Expand All @@ -35,7 +40,8 @@
* @author Aaron Vontell
* @author William Caruso
* @author Byungkyu Park
* @version 0.0.1
* @author Metin Say
* @version 0.0.2
*/
public class AMA {

Expand Down Expand Up @@ -181,7 +187,7 @@ public static void setFont(float size, View ... views) {
/**
* Sets all views to grayscale color
*/
public static void setViewsToGraycasle(Activity activity) {
public static void setViewsToGrayscale(Activity activity) {
List<View> views = getAllViewsAndGroups(activity);
for (View view : views) {
toGrayscale(activity, view, GrayscaleType.AVERAGE);
Expand All @@ -197,9 +203,10 @@ public static void setViewsToGraycasle(Activity activity) {
public static View toGrayscale(Activity activity, View view, GrayscaleType gType) {

if (view instanceof Button) {
int color = ((Button) view).getCurrentTextColor();
((Button) view).setTextColor(colorToGrayscale(color, gType));
((Button) view).setBackgroundResource(android.R.drawable.btn_default);
// TODO: Find out best way to do this
//int color = ((Button) view).getCurrentTextColor();
//((Button) view).setTextColor(colorToGrayscale(color, gType));
//((Button) view).setBackgroundResource(android.);
} else if (view instanceof TextView) {
int color = ((TextView) view).getCurrentTextColor();
((TextView) view).setTextColor(colorToGrayscale(color, gType));
Expand All @@ -208,13 +215,10 @@ public static View toGrayscale(Activity activity, View view, GrayscaleType gType
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
((ImageView) view).setColorFilter(filter);
} else if (view instanceof ViewGroup) {
// do nothing
} else {
int color = 0;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable) {
color = ((ColorDrawable) background).getColor();
int color = ((ColorDrawable) background).getColor();
((ColorDrawable) background).setColor(colorToGrayscale(color, gType));
}
}
Expand All @@ -232,7 +236,9 @@ private static int colorToGrayscale(int color, GrayscaleType gType) {
int[] ARGB = new int[] {(0x11000000 & color) >> 6, (0x110000 & color) >> 4, (0x1100 & color) >> 2, 0x11 & color};
switch (gType) {
case AVERAGE:
return (ARGB[1] + ARGB[2] + ARGB[3]) / 3;
int scale = (ARGB[1] + ARGB[2] + ARGB[3]) / 3;
int result = (ARGB[0] << 6) + (scale << 4) + (scale << 2) + (scale);
return result;
case LIGHTNESS:
return Math.max(ARGB[1], Math.max(ARGB[2], ARGB[3])) + Math.min(ARGB[1], Math.min(ARGB[2], ARGB[3]))/ 2;
case LUMINOSITY:
Expand All @@ -248,10 +254,29 @@ private static int colorToGrayscale(int color, GrayscaleType gType) {
* @param views A list of views to increase the spacing of
*/
public static void increaseSpacing(int space, List<View> views) {
Log.e("INC", "" + views.size());
for(View v : views) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.setMargins(params.leftMargin, params.topMargin + space, params.rightMargin, params.bottomMargin);
v.setLayoutParams(params);

// Check the parent for the corrent params to use
ViewParent parent = v.getParent();

if(parent == null) {
continue;
}

if(parent instanceof RelativeLayout) {

RelativeLayout.MarginLayoutParams params = (RelativeLayout.MarginLayoutParams) v.getLayoutParams();
params.setMargins(params.leftMargin, params.topMargin + space, params.rightMargin, params.bottomMargin);
v.setLayoutParams(params);

} else if (parent instanceof LinearLayout) {

LinearLayout.MarginLayoutParams params = (LinearLayout.MarginLayoutParams) v.getLayoutParams();
params.setMargins(params.leftMargin, params.topMargin + space, params.rightMargin, params.bottomMargin);
v.setLayoutParams(params);

}
}
}

Expand Down Expand Up @@ -676,11 +701,14 @@ public static List<View> getAllViewsAndGroups(Activity activity) {
// Iterate through view in the queue
while (queue.size() > 0) {
View popped = queue.remove();
views.add(popped);
if (popped instanceof ViewGroup) {
ViewGroup group = (ViewGroup) popped;
for (int i = 0; i < group.getChildCount(); i++) {
queue.add(group.getChildAt(i));
}
} else {
views.add(popped);
}
}

Expand Down Expand Up @@ -730,16 +758,38 @@ public static void setSimpleStringAlternatives(Activity activity, Map strings) {
for (View view : views) {
if (view instanceof TextView) {
String text = ((TextView) view).getText().toString();

// Tracking for restoration
boolean simplified = false;
String copy = "" + text;

Set<String> stringKeySet = strings.keySet();
for (String stringKey : stringKeySet) {
if (text.contains(stringKey)) {
simplified = true;
String[] textSplitArray = text.split(stringKey);
text = TextUtils.join(strings.get(stringKey).toString(), textSplitArray);
}
}
((TextView) view).setText(text);
if(simplified) {Store.saveViewString(view, copy);}
}
}
}

/**
* Restores all changes made by <code>setSimpleStringAlternatives()</code>
* @param activity The activity with the views to restore
*/
public static void restoreSimpleStringAlternatives(Activity activity) {

List<View> views = getAllViews(activity);
for (View view : views) {
if (view instanceof TextView) {
Store.restoreString(view);
}
}

}

/**
Expand Down
47 changes: 47 additions & 0 deletions ama/src/main/java/edu/mit/dig/ama/core/Store.java
@@ -0,0 +1,47 @@
package edu.mit.dig.ama.core;

import android.view.View;
import android.widget.TextView;

import java.util.HashMap;

/**
* A class used to store information for either 1) switching between accessible
* modes, or 2) saving accessibility options for use in other applications
* TODO: This class needs to actually store key-val pairs, not to variables
* (variables are for demo only)
* @author Aaron Vontell
* @author William Caruso
* @author Byungkyu Park
* @author Metin Say
* @version 0.0.1
*/
public class Store {

private static HashMap<View, String> originals = new HashMap<>();

/**
* Saves the original text from a string for use in replacing complex
* strings with more complicated strings
* @param view the view which had the original string
* @param original the original string to restore
*/
public static void saveViewString(View view, String original) {
originals.put(view, original);
}

/**
* Restores the view's original text
* TODO: This will be bad if the view was actually changed by the user!
* @param view The view to restore the original content
*/
public static void restoreString(View view) {

if(originals.containsKey(view)) {
((TextView) view).setText(originals.get(view));
}

}


}
4 changes: 2 additions & 2 deletions ama/src/main/res/values/colors.xml
Expand Up @@ -3,9 +3,9 @@

<color name="action_class_white">#FFFFFF</color>
<color name="action_class_green">#5CB85C</color>
<color name="action_class_red">#D9534F</color>
<color name="action_class_red">#d9534f</color>
<color name="action_class_info">#5BC0DE</color>
<color name="action_class_warning">#F0AD4E</color>
<color name="action_class_primary">#0275D8</color>
<color name="action_class_primary">#0275d8</color>

</resources>

0 comments on commit be63c41

Please sign in to comment.