1 package js.kbars; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.graphics.Rect; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.View.OnSystemUiVisibilityChangeListener; 11 import android.view.ViewGroup; 12 import android.widget.Button; 13 import android.widget.CheckBox; 14 import android.widget.LinearLayout; 15 import android.widget.TextView; 16 import java.lang.reflect.Field; 17 import java.util.ArrayList; 18 import java.util.Collections; 19 import java.util.List; 20 21 public class FitSystemWindowsActivity extends Activity { 22 private static final int SYSTEM_UI_CLEARABLE_FLAGS = 7; 23 private static final int SYSTEM_UI_FLAG_ALLOW_TRANSIENT = 2048; 24 private static final int SYSTEM_UI_FLAG_TRANSPARENT_NAVIGATION = 8192; 25 private static final int SYSTEM_UI_FLAG_TRANSPARENT_STATUS = 4096; 26 private final Context mContext = this; 27 private FlagStateTextView mFlagStateTextView; 28 private final Handler mHandler = new Handler(); 29 private LinearLayout mLayout; 30 private final Runnable mUpdateText = new Runnable() { 31 public void run() { 32 FitSystemWindowsActivity.this.mFlagStateTextView.updateText(); 33 } 34 }; 35 36 private static class Flag implements Comparable<Flag> { 37 public static Flag[] ALL = find(); 38 public String caption; 39 public int value; 40 Flag()41 private Flag() { 42 } 43 find()44 private static Flag[] find() { 45 List<Flag> flags = new ArrayList(); 46 String prefix = "SYSTEM_UI_FLAG_"; 47 for (Field f : View.class.getFields()) { 48 if (f.getName().startsWith(prefix)) { 49 Flag flag = new Flag(); 50 flag.caption = f.getName().substring(prefix.length()).replace("NAVIGATION", "NAV").replace("TRANSPARENT", "TRANSP").replace("LAYOUT", "LAY"); 51 try { 52 flag.value = f.getInt(null); 53 if (flag.value != 0) { 54 flags.add(flag); 55 } 56 } catch (Throwable t) { 57 RuntimeException runtimeException = new RuntimeException(t); 58 } 59 } 60 } 61 Collections.sort(flags); 62 return (Flag[]) flags.toArray(new Flag[flags.size()]); 63 } 64 compareTo(Flag another)65 public int compareTo(Flag another) { 66 if (this.value < another.value) { 67 return -1; 68 } 69 return this.value > another.value ? 1 : 0; 70 } 71 } 72 73 private class FlagCheckBox extends CheckBox { 74 private final Flag mFlag; 75 FlagCheckBox(Context context, Flag flag)76 public FlagCheckBox(Context context, Flag flag) { 77 super(context); 78 setText(flag.caption); 79 this.mFlag = flag; 80 } 81 } 82 83 private class FlagSetButton extends Button { FlagSetButton(Context context)84 public FlagSetButton(Context context) { 85 super(context); 86 setText("Set visibility"); 87 setOnClickListener(new OnClickListener() { 88 public void onClick(View v) { 89 FlagSetButton.this.set(); 90 } 91 }); 92 } 93 set()94 private void set() { 95 ViewGroup parent = (ViewGroup) getParent(); 96 int n = parent.getChildCount(); 97 int vis = 0; 98 for (int i = 0; i < n; i++) { 99 View v = parent.getChildAt(i); 100 if (v instanceof FlagCheckBox) { 101 FlagCheckBox cb = (FlagCheckBox) v; 102 if (cb.isChecked()) { 103 vis |= cb.mFlag.value; 104 } 105 } 106 } 107 FitSystemWindowsActivity.this.update(vis); 108 } 109 } 110 111 private class FlagStateTextView extends TextView { FlagStateTextView(Context context)112 public FlagStateTextView(Context context) { 113 super(context); 114 updateText(); 115 setOnClickListener(new OnClickListener() { 116 public void onClick(View v) { 117 FlagStateTextView.this.updateText(); 118 } 119 }); 120 } 121 updateText()122 private void updateText() { 123 StringBuilder sb = new StringBuilder(); 124 append(sb, "vis", FitSystemWindowsActivity.this.mLayout.getSystemUiVisibility()); 125 append(sb, "win", getWindowSystemUiVisibility()); 126 setText(sb.toString()); 127 } 128 append(StringBuilder sb, String caption, int vis)129 private void append(StringBuilder sb, String caption, int vis) { 130 if (sb.length() > 0) { 131 sb.append('\n'); 132 } 133 sb.append(caption).append(':'); 134 boolean first = true; 135 for (Flag flag : Flag.ALL) { 136 if ((flag.value & vis) != 0) { 137 if (first) { 138 first = false; 139 } else { 140 sb.append(", "); 141 } 142 sb.append(flag.caption); 143 } 144 } 145 } 146 } 147 onCreate(Bundle savedInstanceState)148 protected void onCreate(Bundle savedInstanceState) { 149 super.onCreate(savedInstanceState); 150 this.mLayout = new LinearLayout(this.mContext) { 151 protected boolean fitSystemWindows(Rect insets) { 152 Rect insetsBefore = new Rect(insets); 153 String msg = String.format("before=%s\nrt=%s\nafter=%s", new Object[]{insetsBefore.toShortString(), Boolean.valueOf(super.fitSystemWindows(insets)), insets.toShortString()}); 154 return super.fitSystemWindows(insets); 155 } 156 }; 157 this.mLayout.setOrientation(1); 158 for (Flag flag : Flag.ALL) { 159 this.mLayout.addView(new FlagCheckBox(this.mContext, flag)); 160 } 161 this.mLayout.addView(new FlagSetButton(this.mContext)); 162 LinearLayout linearLayout = this.mLayout; 163 View flagStateTextView = new FlagStateTextView(this.mContext); 164 this.mFlagStateTextView = (FlagStateTextView) flagStateTextView; 165 linearLayout.addView(flagStateTextView); 166 this.mLayout.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() { 167 public void onSystemUiVisibilityChange(int vis) { 168 FitSystemWindowsActivity.this.updateTextDelayed(); 169 } 170 }); 171 this.mLayout.setPadding(0, 146, 0, 0); 172 setContentView(this.mLayout); 173 } 174 update(int vis)175 private void update(int vis) { 176 this.mLayout.setSystemUiVisibility(vis); 177 updateTextDelayed(); 178 } 179 updateTextDelayed()180 private void updateTextDelayed() { 181 this.mHandler.removeCallbacks(this.mUpdateText); 182 this.mHandler.postDelayed(this.mUpdateText, 500); 183 } 184 } 185