• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.drawable.Drawable;
23 import android.text.style.ImageSpan;
24 
25 /**
26  * An extension of ImageSpan which adds a padding before the image.
27  */
28 public class RestrictedLockImageSpan extends ImageSpan {
29     private Context mContext;
30     private final float mExtraPadding;
31     private final Drawable mRestrictedPadlock;
32 
RestrictedLockImageSpan(Context context)33     public RestrictedLockImageSpan(Context context) {
34         // we are overriding getDrawable, so passing null to super class here.
35         super((Drawable) null);
36 
37         mContext = context;
38         mExtraPadding = mContext.getResources().getDimensionPixelSize(
39                 R.dimen.restricted_icon_padding);
40         mRestrictedPadlock = RestrictedLockUtils.getRestrictedPadlock(mContext);
41     }
42 
43     @Override
getDrawable()44     public Drawable getDrawable() {
45         return mRestrictedPadlock;
46     }
47 
48     @Override
draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)49     public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
50             int bottom, Paint paint) {
51         Drawable drawable = getDrawable();
52         canvas.save();
53 
54         // Add extra padding before the padlock.
55         float transX = x + mExtraPadding;
56         float transY = (bottom - drawable.getBounds().bottom) / 2.0f;
57 
58         canvas.translate(transX, transY);
59         drawable.draw(canvas);
60         canvas.restore();
61     }
62 
63     @Override
getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetrics)64     public int getSize(Paint paint, CharSequence text, int start, int end,
65             Paint.FontMetricsInt fontMetrics) {
66         int size = super.getSize(paint, text, start, end, fontMetrics);
67         size += 2 * mExtraPadding;
68         return size;
69     }
70 }