1b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock/* 2b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * Copyright (C) 2014 The Android Open Source Project 3b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * 4b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License"); 5b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * you may not use this file except in compliance with the License. 6b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * You may obtain a copy of the License at 7b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * 8b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * http://www.apache.org/licenses/LICENSE-2.0 9b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * 10b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * Unless required by applicable law or agreed to in writing, software 11b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS, 12b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * See the License for the specific language governing permissions and 14b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock * limitations under the License. 15b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock */ 16b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 17b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockpackage com.android.systemui.qs; 18b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 19b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.content.Context; 205e9049a362016c9f00823346d619303674f9df0eJohn Spurlockimport android.content.res.Resources; 21b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.graphics.Canvas; 22b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.graphics.Paint; 23b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.graphics.RectF; 24b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.util.AttributeSet; 25b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport android.view.View; 26b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 27b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockimport com.android.systemui.R; 28b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 29b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlockpublic class DataUsageGraph extends View { 30b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 315e9049a362016c9f00823346d619303674f9df0eJohn Spurlock private final int mTrackColor; 32b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private final int mUsageColor; 335e9049a362016c9f00823346d619303674f9df0eJohn Spurlock private final int mOverlimitColor; 340afb70c66de1fe6b1308656938d4355e707412e5John Spurlock private final int mWarningColor; 355e9049a362016c9f00823346d619303674f9df0eJohn Spurlock private final int mMarkerWidth; 36b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private final RectF mTmpRect = new RectF(); 37b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private final Paint mTmpPaint = new Paint(); 38b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 39b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private long mLimitLevel; 40b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private long mWarningLevel; 41b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock private long mUsageLevel; 420afb70c66de1fe6b1308656938d4355e707412e5John Spurlock private long mMaxLevel; 43b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 44b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock public DataUsageGraph(Context context, AttributeSet attrs) { 45b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock super(context, attrs); 465e9049a362016c9f00823346d619303674f9df0eJohn Spurlock final Resources res = context.getResources(); 474a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette mTrackColor = context.getColor(R.color.data_usage_graph_track); 484a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette mUsageColor = context.getColor(R.color.system_accent_color); 494a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette mOverlimitColor = context.getColor(R.color.system_warning_color); 504a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette mWarningColor = context.getColor(R.color.data_usage_graph_warning); 515e9049a362016c9f00823346d619303674f9df0eJohn Spurlock mMarkerWidth = res.getDimensionPixelSize(R.dimen.data_usage_graph_marker_width); 52b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock } 53b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 540afb70c66de1fe6b1308656938d4355e707412e5John Spurlock public void setLevels(long limitLevel, long warningLevel, long usageLevel) { 550afb70c66de1fe6b1308656938d4355e707412e5John Spurlock mLimitLevel = Math.max(0, limitLevel); 560afb70c66de1fe6b1308656938d4355e707412e5John Spurlock mWarningLevel = Math.max(0, warningLevel); 570afb70c66de1fe6b1308656938d4355e707412e5John Spurlock mUsageLevel = Math.max(0, usageLevel); 580afb70c66de1fe6b1308656938d4355e707412e5John Spurlock mMaxLevel = Math.max(Math.max(Math.max(mLimitLevel, mWarningLevel), mUsageLevel), 1); 59b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock postInvalidate(); 60b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock } 61b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 62b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock @Override 63b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock protected void onDraw(Canvas canvas) { 64b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock super.onDraw(canvas); 65b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 66b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock final RectF r = mTmpRect; 67b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock final Paint p = mTmpPaint; 68b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock final int w = getWidth(); 69b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock final int h = getHeight(); 70b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock 710afb70c66de1fe6b1308656938d4355e707412e5John Spurlock final boolean overLimit = mLimitLevel > 0 && mUsageLevel > mLimitLevel; 720afb70c66de1fe6b1308656938d4355e707412e5John Spurlock float usageRight = w * (mUsageLevel / (float) mMaxLevel); 735e9049a362016c9f00823346d619303674f9df0eJohn Spurlock if (overLimit) { 740afb70c66de1fe6b1308656938d4355e707412e5John Spurlock // compute the gap 750afb70c66de1fe6b1308656938d4355e707412e5John Spurlock usageRight = w * (mLimitLevel / (float) mMaxLevel) - (mMarkerWidth / 2); 760afb70c66de1fe6b1308656938d4355e707412e5John Spurlock usageRight = Math.min(Math.max(usageRight, mMarkerWidth), w - mMarkerWidth * 2); 770afb70c66de1fe6b1308656938d4355e707412e5John Spurlock 780afb70c66de1fe6b1308656938d4355e707412e5John Spurlock // draw overlimit 790afb70c66de1fe6b1308656938d4355e707412e5John Spurlock r.set(usageRight + mMarkerWidth, 0, w, h); 800afb70c66de1fe6b1308656938d4355e707412e5John Spurlock p.setColor(mOverlimitColor); 810afb70c66de1fe6b1308656938d4355e707412e5John Spurlock canvas.drawRect(r, p); 820afb70c66de1fe6b1308656938d4355e707412e5John Spurlock } else { 830afb70c66de1fe6b1308656938d4355e707412e5John Spurlock // draw track 840afb70c66de1fe6b1308656938d4355e707412e5John Spurlock r.set(0, 0, w, h); 850afb70c66de1fe6b1308656938d4355e707412e5John Spurlock p.setColor(mTrackColor); 860afb70c66de1fe6b1308656938d4355e707412e5John Spurlock canvas.drawRect(r, p); 875e9049a362016c9f00823346d619303674f9df0eJohn Spurlock } 885e9049a362016c9f00823346d619303674f9df0eJohn Spurlock 89b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock // draw usage 905e9049a362016c9f00823346d619303674f9df0eJohn Spurlock r.set(0, 0, usageRight, h); 91b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock p.setColor(mUsageColor); 92b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock canvas.drawRect(r, p); 935e9049a362016c9f00823346d619303674f9df0eJohn Spurlock 940afb70c66de1fe6b1308656938d4355e707412e5John Spurlock // draw warning marker 950afb70c66de1fe6b1308656938d4355e707412e5John Spurlock float warningLeft = w * (mWarningLevel / (float) mMaxLevel) - mMarkerWidth / 2; 960afb70c66de1fe6b1308656938d4355e707412e5John Spurlock warningLeft = Math.min(Math.max(warningLeft, 0), w - mMarkerWidth); 970afb70c66de1fe6b1308656938d4355e707412e5John Spurlock r.set(warningLeft, 0, warningLeft + mMarkerWidth, h); 980afb70c66de1fe6b1308656938d4355e707412e5John Spurlock p.setColor(mWarningColor); 990afb70c66de1fe6b1308656938d4355e707412e5John Spurlock canvas.drawRect(r, p); 100b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock } 101b98f747c714ff8252471ca0a3295c2cb9ccb4f3aJohn Spurlock} 102