OSDN Git Service

Remove unnecessary empty space when certain system tray icons aren't showing
authorBraden Farmer <farmerbb@gmail.com>
Sat, 14 Nov 2020 08:03:25 +0000 (01:03 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Sat, 14 Nov 2020 08:03:25 +0000 (01:03 -0700)
app/src/main/java/com/farmerbb/taskbar/ui/TaskbarController.java
app/src/main/java/com/farmerbb/taskbar/util/U.java
app/src/main/res/layout/tb_system_tray.xml
app/src/main/res/layout/tb_taskbar_left.xml
app/src/main/res/layout/tb_taskbar_right.xml
app/src/main/res/layout/tb_taskbar_top_vertical.xml
app/src/main/res/layout/tb_taskbar_vertical.xml
app/src/main/res/values/dimens.xml

index 3e6f0e8..25108bb 100644 (file)
@@ -77,6 +77,7 @@ import android.widget.ImageView;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
@@ -119,6 +120,7 @@ public class TaskbarController extends UIController {
     private TextView time;
     private ImageView notificationCountCircle;
     private TextView notificationCountText;
+    private boolean[] sysTrayIconStates = { false, false, false, false, false };
 
     private Handler handler;
     private Handler handler2;
@@ -152,6 +154,14 @@ public class TaskbarController extends UIController {
     private int numOfPinnedApps = -1;
 
     private int cellStrength = -1;
+    private int notificationCount = 0;
+    private int numOfSysTrayIcons = 0;
+
+    private final int BATTERY = 0;
+    private final int WIFI = 1;
+    private final int CELLULAR = 2;
+    private final int BLUETOOTH = 3;
+    private final int NOTIFICATIONS = 4;
 
     private View.OnClickListener ocl = view ->
             U.sendBroadcast(context, ACTION_TOGGLE_START_MENU);
@@ -205,20 +215,7 @@ public class TaskbarController extends UIController {
         @SuppressLint("SetTextI18n")
         @Override
         public void onReceive(Context context, Intent intent) {
-            int count = intent.getIntExtra(EXTRA_COUNT, 0);
-            if(count > 0) {
-                int color = ColorUtils.setAlphaComponent(U.getBackgroundTint(context), 255);
-                notificationCountText.setTextColor(color);
-
-                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.tb_circle);
-                drawable.setTint(U.getAccentColor(context));
-
-                notificationCountCircle.setImageDrawable(drawable);
-                notificationCountText.setText(Integer.toString(count));
-            } else {
-                notificationCountCircle.setImageDrawable(null);
-                notificationCountText.setText(null);
-            }
+            notificationCount = intent.getIntExtra(EXTRA_COUNT, 0);
         }
     };
 
@@ -733,7 +730,7 @@ public class TaskbarController extends UIController {
                     updateSystemTray();
                     updateRecentApps(false);
 
-                    if(showHideAutomagically && !positionIsVertical && !MenuHelper.getInstance().isStartMenuOpen())
+                    if(showHideAutomagically && !positionIsVertical && !MenuHelper.getInstance().isStartMenuOpen()) {
                         handler.post(() -> {
                             if(layout != null) {
                                 int[] location = new int[2];
@@ -753,6 +750,7 @@ public class TaskbarController extends UIController {
                                 }
                             }
                         });
+                    }
 
                     SystemClock.sleep(refreshInterval);
                 }
@@ -890,8 +888,14 @@ public class TaskbarController extends UIController {
                 finalApplicationIds.add(entry.getPackageName());
             }
 
+            int realNumOfSysTrayIcons = 0;
+            for(Boolean state : sysTrayIconStates) {
+                if(state) realNumOfSysTrayIcons++;
+            }
+
             if(finalApplicationIds.size() != currentTaskbarIds.size()
-                    || numOfPinnedApps != realNumOfPinnedApps)
+                    || numOfPinnedApps != realNumOfPinnedApps
+                    || numOfSysTrayIcons != realNumOfSysTrayIcons)
                 shouldRedrawTaskbar = true;
             else {
                 for(int i = 0; i < finalApplicationIds.size(); i++) {
@@ -905,6 +909,7 @@ public class TaskbarController extends UIController {
             if(shouldRedrawTaskbar) {
                 currentTaskbarIds = finalApplicationIds;
                 numOfPinnedApps = realNumOfPinnedApps;
+                numOfSysTrayIcons = realNumOfSysTrayIcons;
 
                 populateAppEntries(context, pm, entries, launcherAppCache);
 
@@ -916,6 +921,26 @@ public class TaskbarController extends UIController {
                         calculateScrollViewParams(context, pref, params, fullLength, numOfEntries);
                         scrollView.setLayoutParams(params);
 
+                        sysTrayLayout.findViewById(R.id.battery).setVisibility(
+                                sysTrayIconStates[BATTERY] ? View.VISIBLE : View.GONE
+                        );
+
+                        sysTrayLayout.findViewById(R.id.wifi).setVisibility(
+                                sysTrayIconStates[WIFI] ? View.VISIBLE : View.GONE
+                        );
+
+                        sysTrayLayout.findViewById(R.id.cellular).setVisibility(
+                                sysTrayIconStates[CELLULAR] ? View.VISIBLE : View.GONE
+                        );
+
+                        sysTrayLayout.findViewById(R.id.bluetooth).setVisibility(
+                                sysTrayIconStates[BLUETOOTH] ? View.VISIBLE : View.GONE
+                        );
+
+                        sysTrayLayout.findViewById(R.id.notification_count_layout).setVisibility(
+                                sysTrayIconStates[NOTIFICATIONS] ? View.VISIBLE : View.GONE
+                        );
+
                         taskbar.removeAllViews();
                         for(int i = 0; i < entries.size(); i++) {
                             taskbar.addView(getView(entries, i));
@@ -932,7 +957,7 @@ public class TaskbarController extends UIController {
                                 scrollView.setVisibility(View.VISIBLE);
                         }
 
-                        if(firstRefresh && scrollView.getVisibility() != View.VISIBLE)
+                        if(firstRefresh && scrollView.getVisibility() != View.VISIBLE) {
                             new Handler().post(
                                     () -> scrollTaskbar(
                                             scrollView,
@@ -942,6 +967,7 @@ public class TaskbarController extends UIController {
                                             shouldRefreshRecents
                                     )
                             );
+                        }
                     } else {
                         isShowingRecents = false;
                         scrollView.setVisibility(View.GONE);
@@ -965,14 +991,13 @@ public class TaskbarController extends UIController {
                                    boolean fullLength,
                                    int numOfEntries) {
         DisplayInfo display = U.getDisplayInfo(context, true);
-        int recentsSize =
-                context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size) * numOfEntries;
+        int recentsSize = context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size) * numOfEntries;
         float maxRecentsSize = fullLength ? Float.MAX_VALUE : recentsSize;
 
         if (TaskbarPosition.isVertical(context)) {
             int maxScreenSize = Math.max(0, display.height
                     - U.getStatusBarHeight(context)
-                    - U.getBaseTaskbarSize(context));
+                    - U.getBaseTaskbarSize(context, sysTrayIconStates));
 
             params.height = (int) Math.min(maxRecentsSize, maxScreenSize)
                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
@@ -1003,7 +1028,7 @@ public class TaskbarController extends UIController {
                 } catch (NullPointerException ignored) {}
             }
         } else {
-            int maxScreenSize = Math.max(0, display.width - U.getBaseTaskbarSize(context));
+            int maxScreenSize = Math.max(0, display.width - U.getBaseTaskbarSize(context, sysTrayIconStates));
 
             params.width = (int) Math.min(maxRecentsSize, maxScreenSize)
                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
@@ -1674,17 +1699,39 @@ public class TaskbarController extends UIController {
         if(!sysTrayEnabled || isScreenOff()) return;
 
         handler.post(() -> {
+            Drawable batteryDrawable = getBatteryDrawable();
+            Drawable wifiDrawable = getWifiDrawable();
+            Drawable bluetoothDrawable = getBluetoothDrawable();
+            Drawable cellularDrawable = getCellularDrawable();
+
             ImageView battery = sysTrayLayout.findViewById(R.id.battery);
-            battery.setImageDrawable(getBatteryDrawable());
+            if(batteryDrawable != null) battery.setImageDrawable(batteryDrawable);
+            sysTrayIconStates[BATTERY] = batteryDrawable != null;
 
             ImageView wifi = sysTrayLayout.findViewById(R.id.wifi);
-            wifi.setImageDrawable(getWifiDrawable());
+            if(wifiDrawable != null) wifi.setImageDrawable(wifiDrawable);
+            sysTrayIconStates[WIFI] = wifiDrawable != null;
 
             ImageView bluetooth = sysTrayLayout.findViewById(R.id.bluetooth);
-            bluetooth.setImageDrawable(getBluetoothDrawable());
+            if(bluetoothDrawable != null) bluetooth.setImageDrawable(bluetoothDrawable);
+            sysTrayIconStates[BLUETOOTH] = bluetoothDrawable != null;
 
             ImageView cellular = sysTrayLayout.findViewById(R.id.cellular);
-            cellular.setImageDrawable(getCellularDrawable());
+            if(cellularDrawable != null) cellular.setImageDrawable(cellularDrawable);
+            sysTrayIconStates[CELLULAR] = cellularDrawable != null;
+
+            if(notificationCount > 0) {
+                int color = ColorUtils.setAlphaComponent(U.getBackgroundTint(context), 255);
+                notificationCountText.setTextColor(color);
+
+                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.tb_circle);
+                drawable.setTint(U.getAccentColor(context));
+
+                notificationCountCircle.setImageDrawable(drawable);
+                notificationCountText.setText(Integer.toString(notificationCount));
+                sysTrayIconStates[NOTIFICATIONS] = true;
+            } else
+                sysTrayIconStates[NOTIFICATIONS] = false;
 
             time.setText(context.getString(R.string.tb_systray_clock,
                     DateFormat.getTimeFormat(context).format(new Date()),
index 406da4c..c0952a5 100644 (file)
@@ -98,6 +98,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 import static com.farmerbb.taskbar.util.Constants.*;
@@ -1139,11 +1140,15 @@ public class U {
         }
     }
 
-    public static int getBaseTaskbarSize(Context context) {
-        return Math.round(getBaseTaskbarSizeFloat(context));
+    public static int getBaseTaskbarSize(Context context, boolean[] sysTrayIconStates) {
+        return Math.round(getBaseTaskbarSizeFloat(context, sysTrayIconStates));
     }
 
     private static float getBaseTaskbarSizeFloat(Context context) {
+        return getBaseTaskbarSizeFloat(context, new boolean[] {});
+    }
+
+    private static float getBaseTaskbarSizeFloat(Context context, boolean[] sysTrayIconStates) {
         SharedPreferences pref = getSharedPreferences(context);
         float baseTaskbarSize = context.getResources().getDimension(R.dimen.tb_base_taskbar_size);
         boolean navbarButtonsEnabled = false;
@@ -1169,8 +1174,16 @@ public class U {
         if(navbarButtonsEnabled)
             baseTaskbarSize += context.getResources().getDimension(R.dimen.tb_navbar_buttons_margin);
 
-        if(isSystemTrayEnabled(context))
-            baseTaskbarSize += context.getResources().getDimension(R.dimen.tb_systray_size);
+        if(isSystemTrayEnabled(context)) {
+            float sysTraySize = context.getResources().getDimension(R.dimen.tb_systray_size);
+
+            for(boolean state : sysTrayIconStates) {
+                if(!state) sysTraySize -= context.getResources().getDimension(R.dimen.tb_systray_icon_size);
+            }
+
+            baseTaskbarSize += sysTraySize;
+        }
+
 
         return baseTaskbarSize;
     }
index 2b82fda..99fb2ca 100644 (file)
@@ -30,6 +30,7 @@
         tools:ignore="SpUsage" />
 
     <FrameLayout
+        android:id="@+id/notification_count_layout"
         android:layout_width="24dp"
         android:layout_height="24dp"
         android:layout_gravity="center_vertical">
index 67dd1b7..a977f23 100644 (file)
 
         <FrameLayout
             android:id="@+id/add_systray_here"
-            android:layout_width="@dimen/tb_systray_size"
+            android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:visibility="gone"/>
 
index 5fdbfaf..1d6fcda 100644 (file)
@@ -33,7 +33,7 @@
 
         <FrameLayout
             android:id="@+id/add_systray_here"
-            android:layout_width="@dimen/tb_systray_size"
+            android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:visibility="gone"/>
 
index 750e7b8..741c379 100644 (file)
     <FrameLayout
         android:id="@+id/add_systray_here"
         android:layout_width="wrap_content"
-        android:layout_height="@dimen/tb_systray_size"
+        android:layout_height="wrap_content"
         android:visibility="gone"/>
 
     <Space
index 2115b0e..e6c8131 100644 (file)
@@ -45,7 +45,7 @@
     <FrameLayout
         android:id="@+id/add_systray_here"
         android:layout_width="wrap_content"
-        android:layout_height="@dimen/tb_systray_size"
+        android:layout_height="wrap_content"
         android:visibility="gone"/>
 
     <ScrollView
index 361efad..7b09b2f 100644 (file)
@@ -43,4 +43,5 @@
     <dimen name="tb_fake_fab_padding">16dp</dimen>
     <dimen name="tb_settings_icon_size">24dp</dimen>
     <dimen name="tb_qs_icon_preview_size">32dp</dimen>
+    <dimen name="tb_systray_icon_size">24dp</dimen>
 </resources>