Make LinearColorBar support RTL

- Move the class to widget package
- Remove a bunch of useless drawing

Change-Id: I82f840819c89eaf3cf9b7a87e56f1031989e3262
Fixes: 62700884
Test:  robotest
diff --git a/src/com/android/settings/SummaryPreference.java b/src/com/android/settings/SummaryPreference.java
index d129661..23965ee 100644
--- a/src/com/android/settings/SummaryPreference.java
+++ b/src/com/android/settings/SummaryPreference.java
@@ -21,7 +21,8 @@
 import android.util.AttributeSet;
 import android.view.View;
 import android.widget.TextView;
-import com.android.settings.applications.LinearColorBar;
+
+import com.android.settings.widget.LinearColorBar;
 
 /**
  * Provides a summary of a setting page in a preference.  Such as memory or data usage.
@@ -92,7 +93,7 @@
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
 
-        LinearColorBar colorBar = (LinearColorBar) holder.itemView.findViewById(R.id.color_bar);
+        final LinearColorBar colorBar = holder.itemView.findViewById(R.id.color_bar);
 
         if (mChartEnabled) {
             colorBar.setVisibility(View.VISIBLE);
diff --git a/src/com/android/settings/applications/LinearColorBar.java b/src/com/android/settings/applications/LinearColorBar.java
deleted file mode 100644
index b637671..0000000
--- a/src/com/android/settings/applications/LinearColorBar.java
+++ /dev/null
@@ -1,274 +0,0 @@
-/**
- *
- */
-package com.android.settings.applications;
-
-import android.content.Context;
-import android.graphics.Canvas;
-import android.graphics.LinearGradient;
-import android.graphics.Paint;
-import android.graphics.Path;
-import android.graphics.Rect;
-import android.graphics.Shader;
-import android.util.AttributeSet;
-import android.util.DisplayMetrics;
-import android.view.MotionEvent;
-import android.widget.LinearLayout;
-import com.android.settings.Utils;
-
-public class LinearColorBar extends LinearLayout {
-
-    static final int RIGHT_COLOR = 0xffced7db;
-    static final int GRAY_COLOR = 0xff555555;
-    static final int WHITE_COLOR = 0xffffffff;
-
-    private float mRedRatio;
-    private float mYellowRatio;
-    private float mGreenRatio;
-
-    private int mLeftColor;
-    private int mMiddleColor;
-    private int mRightColor = RIGHT_COLOR;
-
-    private boolean mShowIndicator = true;
-    private boolean mShowingGreen;
-
-    private OnRegionTappedListener mOnRegionTappedListener;
-    private int mColoredRegions = REGION_RED | REGION_YELLOW | REGION_GREEN;
-
-    final Rect mRect = new Rect();
-    final Paint mPaint = new Paint();
-
-    int mLastInterestingLeft, mLastInterestingRight;
-    int mLineWidth;
-
-    int mLastLeftDiv, mLastRightDiv;
-    int mLastRegion;
-
-    final Path mColorPath = new Path();
-    final Path mEdgePath = new Path();
-    final Paint mColorGradientPaint = new Paint();
-    final Paint mEdgeGradientPaint = new Paint();
-
-    public static final int REGION_RED = 1<<0;
-    public static final int REGION_YELLOW = 1<<1;
-    public static final int REGION_GREEN = 1<<2;
-    public static final int REGION_ALL = REGION_RED | REGION_YELLOW | REGION_GREEN;
-
-    public interface OnRegionTappedListener {
-        public void onRegionTapped(int region);
-    }
-
-    public LinearColorBar(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        setWillNotDraw(false);
-        mPaint.setStyle(Paint.Style.FILL);
-        mColorGradientPaint.setStyle(Paint.Style.FILL);
-        mColorGradientPaint.setAntiAlias(true);
-        mEdgeGradientPaint.setStyle(Paint.Style.STROKE);
-        mLineWidth = getResources().getDisplayMetrics().densityDpi >= DisplayMetrics.DENSITY_HIGH
-                ? 2 : 1;
-        mEdgeGradientPaint.setStrokeWidth(mLineWidth);
-        mEdgeGradientPaint.setAntiAlias(true);
-        mLeftColor = mMiddleColor = Utils.getColorAccent(context);
-    }
-
-    public void setOnRegionTappedListener(OnRegionTappedListener listener) {
-        if (listener != mOnRegionTappedListener) {
-            mOnRegionTappedListener = listener;
-            setClickable(listener != null);
-        }
-    }
-
-    public void setColoredRegions(int regions) {
-        mColoredRegions = regions;
-        invalidate();
-    }
-
-    public void setRatios(float red, float yellow, float green) {
-        mRedRatio = red;
-        mYellowRatio = yellow;
-        mGreenRatio = green;
-        invalidate();
-    }
-
-    public void setColors(int red, int yellow, int green) {
-        mLeftColor = red;
-        mMiddleColor = yellow;
-        mRightColor = green;
-        updateIndicator();
-        invalidate();
-    }
-
-    public void setShowIndicator(boolean showIndicator) {
-        mShowIndicator = showIndicator;
-        updateIndicator();
-        invalidate();
-    }
-
-    public void setShowingGreen(boolean showingGreen) {
-        if (mShowingGreen != showingGreen) {
-            mShowingGreen = showingGreen;
-            updateIndicator();
-            invalidate();
-        }
-    }
-
-    private void updateIndicator() {
-        int off = getPaddingTop() - getPaddingBottom();
-        if (off < 0) off = 0;
-        mRect.top = off;
-        mRect.bottom = getHeight();
-        if (!mShowIndicator) {
-            return;
-        }
-        if (mShowingGreen) {
-            mColorGradientPaint.setShader(new LinearGradient(
-                    0, 0, 0, off-2, mRightColor &0xffffff, mRightColor, Shader.TileMode.CLAMP));
-        } else {
-            mColorGradientPaint.setShader(new LinearGradient(
-                    0, 0, 0, off-2, mMiddleColor&0xffffff, mMiddleColor, Shader.TileMode.CLAMP));
-        }
-        mEdgeGradientPaint.setShader(new LinearGradient(
-                0, 0, 0, off/2, 0x00a0a0a0, 0xffa0a0a0, Shader.TileMode.CLAMP));
-    }
-
-    @Override
-    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
-        super.onSizeChanged(w, h, oldw, oldh);
-        updateIndicator();
-    }
-
-    @Override
-    public boolean onTouchEvent(MotionEvent event) {
-        if (mOnRegionTappedListener != null) {
-            switch (event.getAction()) {
-                case MotionEvent.ACTION_DOWN: {
-                    final int x = (int) event.getX();
-                    if (x < mLastLeftDiv) {
-                        mLastRegion = REGION_RED;
-                    } else if (x < mLastRightDiv) {
-                        mLastRegion = REGION_YELLOW;
-                    } else {
-                        mLastRegion = REGION_GREEN;
-                    }
-                    invalidate();
-                } break;
-            }
-        }
-        return super.onTouchEvent(event);
-    }
-
-    @Override
-    protected void dispatchSetPressed(boolean pressed) {
-        invalidate();
-    }
-
-    @Override
-    public boolean performClick() {
-        if (mOnRegionTappedListener != null && mLastRegion != 0) {
-            mOnRegionTappedListener.onRegionTapped(mLastRegion);
-            mLastRegion = 0;
-        }
-        return super.performClick();
-    }
-
-    private int pickColor(int color, int region) {
-        if (isPressed() && (mLastRegion&region) != 0) {
-            return WHITE_COLOR;
-        }
-        if ((mColoredRegions&region) == 0) {
-            return GRAY_COLOR;
-        }
-        return color;
-    }
-
-    @Override
-    protected void onDraw(Canvas canvas) {
-        super.onDraw(canvas);
-
-        int width = getWidth();
-
-        int left = 0;
-
-        int right = left + (int)(width*mRedRatio);
-        int right2 = right + (int)(width*mYellowRatio);
-        int right3 = right2 + (int)(width*mGreenRatio);
-
-        int indicatorLeft, indicatorRight;
-        if (mShowingGreen) {
-            indicatorLeft = right2;
-            indicatorRight = right3;
-        } else {
-            indicatorLeft = right;
-            indicatorRight = right2;
-        }
-
-        if (mLastInterestingLeft != indicatorLeft || mLastInterestingRight != indicatorRight) {
-            mColorPath.reset();
-            mEdgePath.reset();
-            if (mShowIndicator && indicatorLeft < indicatorRight) {
-                final int midTopY = mRect.top;
-                final int midBottomY = 0;
-                final int xoff = 2;
-                mColorPath.moveTo(indicatorLeft, mRect.top);
-                mColorPath.cubicTo(indicatorLeft, midBottomY,
-                        -xoff, midTopY,
-                        -xoff, 0);
-                mColorPath.lineTo(width+xoff-1, 0);
-                mColorPath.cubicTo(width+xoff-1, midTopY,
-                        indicatorRight, midBottomY,
-                        indicatorRight, mRect.top);
-                mColorPath.close();
-                final float lineOffset = mLineWidth+.5f;
-                mEdgePath.moveTo(-xoff+lineOffset, 0);
-                mEdgePath.cubicTo(-xoff+lineOffset, midTopY,
-                        indicatorLeft+lineOffset, midBottomY,
-                        indicatorLeft+lineOffset, mRect.top);
-                mEdgePath.moveTo(width+xoff-1-lineOffset, 0);
-                mEdgePath.cubicTo(width+xoff-1-lineOffset, midTopY,
-                        indicatorRight-lineOffset, midBottomY,
-                        indicatorRight-lineOffset, mRect.top);
-            }
-            mLastInterestingLeft = indicatorLeft;
-            mLastInterestingRight = indicatorRight;
-        }
-
-        if (!mEdgePath.isEmpty()) {
-            canvas.drawPath(mEdgePath, mEdgeGradientPaint);
-            canvas.drawPath(mColorPath, mColorGradientPaint);
-        }
-
-        if (left < right) {
-            mRect.left = left;
-            mRect.right = right;
-            mPaint.setColor(pickColor(mLeftColor, REGION_RED));
-            canvas.drawRect(mRect, mPaint);
-            width -= (right-left);
-            left = right;
-        }
-
-        mLastLeftDiv = right;
-        mLastRightDiv = right2;
-
-        right = right2;
-
-        if (left < right) {
-            mRect.left = left;
-            mRect.right = right;
-            mPaint.setColor(pickColor(mMiddleColor, REGION_YELLOW));
-            canvas.drawRect(mRect, mPaint);
-            width -= (right-left);
-            left = right;
-        }
-
-
-        right = left + width;
-        if (left < right) {
-            mRect.left = left;
-            mRect.right = right;
-            mPaint.setColor(pickColor(mRightColor, REGION_GREEN));
-            canvas.drawRect(mRect, mPaint);
-        }
-    }
-}
\ No newline at end of file
diff --git a/src/com/android/settings/applications/RunningProcessesView.java b/src/com/android/settings/applications/RunningProcessesView.java
index b365435..dd8bcf8 100644
--- a/src/com/android/settings/applications/RunningProcessesView.java
+++ b/src/com/android/settings/applications/RunningProcessesView.java
@@ -43,6 +43,7 @@
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
 import com.android.settings.Utils;
+import com.android.settings.widget.LinearColorBar;
 
 import java.util.ArrayList;
 import java.util.Collections;
diff --git a/src/com/android/settings/datausage/DataUsageSummary.java b/src/com/android/settings/datausage/DataUsageSummary.java
index b4d5f50..e626c3a 100644
--- a/src/com/android/settings/datausage/DataUsageSummary.java
+++ b/src/com/android/settings/datausage/DataUsageSummary.java
@@ -20,8 +20,6 @@
 import android.content.Intent;
 import android.net.NetworkPolicyManager;
 import android.net.NetworkTemplate;
-import android.net.wifi.WifiConfiguration;
-import android.net.wifi.WifiManager;
 import android.os.Bundle;
 import android.os.UserManager;
 import android.provider.SearchIndexableResource;
diff --git a/src/com/android/settings/widget/LinearColorBar.java b/src/com/android/settings/widget/LinearColorBar.java
new file mode 100644
index 0000000..b3e685e
--- /dev/null
+++ b/src/com/android/settings/widget/LinearColorBar.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.widget;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+import android.util.DisplayMetrics;
+import android.widget.LinearLayout;
+
+import com.android.settings.Utils;
+
+public class LinearColorBar extends LinearLayout {
+
+    static final int RIGHT_COLOR = 0xffced7db;
+    static final int GRAY_COLOR = 0xff555555;
+    static final int WHITE_COLOR = 0xffffffff;
+
+    private float mRedRatio;
+    private float mYellowRatio;
+    private float mGreenRatio;
+
+    private int mLeftColor;
+    private int mMiddleColor;
+    private int mRightColor = RIGHT_COLOR;
+
+    private int mColoredRegions = REGION_RED | REGION_YELLOW | REGION_GREEN;
+
+    final Rect mRect = new Rect();
+    final Paint mPaint = new Paint();
+
+    int mLineWidth;
+
+    int mLastRegion;
+
+    final Paint mColorGradientPaint = new Paint();
+    final Paint mEdgeGradientPaint = new Paint();
+
+    public static final int REGION_RED = 1 << 0;
+    public static final int REGION_YELLOW = 1 << 1;
+    public static final int REGION_GREEN = 1 << 2;
+
+    public LinearColorBar(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setWillNotDraw(false);
+        mPaint.setStyle(Paint.Style.FILL);
+        mColorGradientPaint.setStyle(Paint.Style.FILL);
+        mColorGradientPaint.setAntiAlias(true);
+        mEdgeGradientPaint.setStyle(Paint.Style.STROKE);
+        mLineWidth = getResources().getDisplayMetrics().densityDpi >= DisplayMetrics.DENSITY_HIGH
+                ? 2 : 1;
+        mEdgeGradientPaint.setStrokeWidth(mLineWidth);
+        mEdgeGradientPaint.setAntiAlias(true);
+        mLeftColor = mMiddleColor = Utils.getColorAccent(context);
+    }
+
+    public void setRatios(float red, float yellow, float green) {
+        mRedRatio = red;
+        mYellowRatio = yellow;
+        mGreenRatio = green;
+        invalidate();
+    }
+
+    public void setColors(int red, int yellow, int green) {
+        mLeftColor = red;
+        mMiddleColor = yellow;
+        mRightColor = green;
+        updateIndicator();
+        invalidate();
+    }
+
+    private void updateIndicator() {
+        int off = getPaddingTop() - getPaddingBottom();
+        if (off < 0) off = 0;
+        mRect.top = off;
+        mRect.bottom = getHeight();
+    }
+
+    @Override
+    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+        super.onSizeChanged(w, h, oldw, oldh);
+        updateIndicator();
+    }
+
+    @Override
+    protected void dispatchSetPressed(boolean pressed) {
+        invalidate();
+    }
+
+    private int pickColor(int color, int region) {
+        if (isPressed() && (mLastRegion & region) != 0) {
+            return WHITE_COLOR;
+        }
+        if ((mColoredRegions & region) == 0) {
+            return GRAY_COLOR;
+        }
+        return color;
+    }
+
+    @Override
+    protected void onDraw(Canvas canvas) {
+        super.onDraw(canvas);
+
+        final int width = getWidth();
+
+        if (!isLayoutRtl()) {
+            drawLtr(canvas, width);
+        } else {
+            drawRtl(canvas, width);
+        }
+    }
+
+    private void drawLtr(Canvas canvas, int width) {
+        int start = 0;
+        int end = start + (int) (width * mRedRatio);
+        int end2 = end + (int) (width * mYellowRatio);
+
+        if (start < end) {
+            mRect.left = start;
+            mRect.right = end;
+            mPaint.setColor(pickColor(mLeftColor, REGION_RED));
+            canvas.drawRect(mRect, mPaint);
+            start = end;
+        }
+
+        end = end2;
+
+        if (start < end) {
+            mRect.left = start;
+            mRect.right = end;
+            mPaint.setColor(pickColor(mMiddleColor, REGION_YELLOW));
+            canvas.drawRect(mRect, mPaint);
+            start = end;
+        }
+
+        end = width;
+        if (start < end) {
+            mRect.left = start;
+            mRect.right = end;
+            mPaint.setColor(pickColor(mRightColor, REGION_GREEN));
+            canvas.drawRect(mRect, mPaint);
+        }
+    }
+
+    private void drawRtl(Canvas canvas, int width) {
+        int start = width;
+        int end = start - (int) (width * mRedRatio);
+        int end2 = end - (int) (width * mYellowRatio);
+
+        if (start > end) {
+            mRect.left = end;
+            mRect.right = start;
+            mPaint.setColor(pickColor(mLeftColor, REGION_RED));
+            canvas.drawRect(mRect, mPaint);
+            start = end;
+        }
+
+        end = end2;
+
+        if (start > end) {
+            mRect.left = end;
+            mRect.right = start;
+            mPaint.setColor(pickColor(mMiddleColor, REGION_YELLOW));
+            canvas.drawRect(mRect, mPaint);
+            start = end;
+        }
+
+        end = 0;
+        if (start > end) {
+            mRect.left = end;
+            mRect.right = start;
+            mPaint.setColor(pickColor(mRightColor, REGION_GREEN));
+            canvas.drawRect(mRect, mPaint);
+        }
+    }
+}
\ No newline at end of file