Skip to content

Commit c35d609

Browse files
committed
QuickSettings: improved tile layout for Android 4.3
- use recursion to find TextView in view hierarchy - adjust ImageView margins according to "Tiles per row" setting
1 parent 5b6e8f0 commit c35d609

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

src/com/ceco/gm2/gravitybox/ModQuickSettings.java

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@
5959
import android.os.Build;
6060
import android.os.IBinder;
6161
import android.provider.Settings;
62+
import android.util.TypedValue;
6263
import android.view.LayoutInflater;
6364
import android.view.MotionEvent;
6465
import android.view.View;
6566
import android.view.View.MeasureSpec;
6667
import android.view.ViewGroup;
6768
import android.widget.FrameLayout;
6869
import android.widget.ImageView;
70+
import android.widget.LinearLayout;
6971
import android.widget.TextView;
7072
import de.robv.android.xposed.XC_MethodHook;
7173
import de.robv.android.xposed.XC_MethodReplacement;
@@ -270,43 +272,98 @@ private static void updateTileOrderAndVisibility() {
270272
XposedHelpers.callMethod(mContainerView, "updateResources");
271273
}
272274

275+
private static TextView findTileTextView(ViewGroup viewGroup) {
276+
if (viewGroup == null) return null;
277+
278+
TextView textView = null;
279+
final int childCount = viewGroup.getChildCount();
280+
for (int i = 0; i < childCount; i++) {
281+
View childView = viewGroup.getChildAt(i);
282+
if (childView instanceof ViewGroup) {
283+
textView = findTileTextView((ViewGroup) childView);
284+
} else if (childView instanceof TextView) {
285+
textView = (TextView) childView;
286+
}
287+
if (textView != null) {
288+
break;
289+
}
290+
}
291+
292+
return textView;
293+
}
294+
273295
private static void updateTileLayout(FrameLayout container, int orientation) {
274296
if (container == null) return;
275297

276298
int tileCount = container.getChildCount();
277299
int textSize = 12;
278300

301+
final Resources res = container.getResources();
302+
int imgMarginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
303+
27, res.getDisplayMetrics());
304+
int imgMarginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
305+
17, res.getDisplayMetrics());
306+
final int imgResId = res.getIdentifier("image", "id", PACKAGE_NAME);
307+
final int rssiImgResId = res.getIdentifier("rssi_image", "id", PACKAGE_NAME);
308+
279309
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
280310
switch (mNumColumns) {
281-
case 4: textSize = 10; break;
282-
case 5: textSize = 8; break;
311+
case 4:
312+
textSize = 10;
313+
imgMarginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
314+
17, res.getDisplayMetrics());
315+
imgMarginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
316+
10, res.getDisplayMetrics());
317+
break;
318+
case 5:
319+
imgMarginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
320+
10, res.getDisplayMetrics());
321+
imgMarginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
322+
5, res.getDisplayMetrics());
323+
textSize = 8;
324+
break;
283325
case 3:
284-
default: textSize = 12;
326+
default:
327+
textSize = 12;
328+
break;
285329
}
286330
}
287331

288332
for(int i = 0; i < tileCount; i++) {
289333
ViewGroup viewGroup = (ViewGroup) mContainerView.getChildAt(i);
290334
if (viewGroup != null) {
291-
int childCount = viewGroup.getChildCount();
292-
for(int j = 0; j < childCount; j++) {
293-
View childView = viewGroup.getChildAt(j);
294-
TextView targetView = null;
295-
if (childView instanceof ViewGroup) {
296-
int innerChildCount = ((ViewGroup) childView).getChildCount();
297-
for (int k = 0; k < innerChildCount; k++) {
298-
View innerChildView = ((ViewGroup) childView).getChildAt(k);
299-
if (innerChildView instanceof TextView) {
300-
targetView = (TextView) innerChildView;
335+
TextView textView = findTileTextView(viewGroup);
336+
if (textView != null) {
337+
textView.setTextSize(1, textSize);
338+
textView.setSingleLine(false);
339+
textView.setAllCaps(true);
340+
}
341+
342+
// adjust layout in case it's AOSP 4.3 tile
343+
if (Build.VERSION.SDK_INT > 17 && imgResId != 0 && rssiImgResId != 0) {
344+
ImageView img = (ImageView) viewGroup.findViewById(imgResId);
345+
if (img != null) {
346+
// basic tile
347+
if (img.getLayoutParams() instanceof LinearLayout.LayoutParams) {
348+
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) img.getLayoutParams();
349+
lp.topMargin = imgMarginTop;
350+
lp.bottomMargin = imgMarginBottom;
351+
img.setLayoutParams(lp);
352+
img.requestLayout();
353+
}
354+
} else {
355+
// RSSI special tile
356+
img = (ImageView) viewGroup.findViewById(rssiImgResId);
357+
if (img != null && img.getParent() instanceof FrameLayout) {
358+
FrameLayout fl = (FrameLayout) img.getParent();
359+
if (fl.getLayoutParams() instanceof LinearLayout.LayoutParams) {
360+
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) fl.getLayoutParams();
361+
lp.topMargin = imgMarginTop;
362+
lp.bottomMargin = imgMarginBottom;
363+
fl.setLayoutParams(lp);
364+
fl.requestLayout();
301365
}
302366
}
303-
} else if (childView instanceof TextView) {
304-
targetView = (TextView) childView;
305-
}
306-
if (targetView != null) {
307-
targetView.setTextSize(1, textSize);
308-
targetView.setSingleLine(false);
309-
targetView.setAllCaps(true);
310367
}
311368
}
312369
}

0 commit comments

Comments
 (0)