summaryrefslogtreecommitdiff
path: root/src/com/android/mms/ui/SubscriptionView.java
blob: b21cc558985a02d3c980a922f979cc71d5a44fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2014 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.mms.ui;

import android.content.Context;
import android.telephony.SubscriptionManager;
import android.telephony.SubInfoRecord;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.mms.R;

public class SubscriptionView extends LinearLayout {

    public static final int DARK_THEME = 0;
    public static final int LIGHT_THEME = 1;
    private static final int MIN_NUM_LENGTH = 4;

    private TextView mSubNameView;
    private TextView mSubNumView;
    private TextView mSubShortNumView;
    private RelativeLayout mSubColorView;
    private int mThemeType = DARK_THEME;
    private int mNumLength = MIN_NUM_LENGTH;

    public SubscriptionView(Context context) {
        this(context,null);
    }

    public SubscriptionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflator.inflate(R.layout.subscription_item_layout, null);
        addView(view);
        initViewElement(view);
    }

    /**
     * Set how many numbers to be shown for short number view, default is MIN_NUM_LENGTH which is 4
     * @param numLength The length of subscription number to be shown
     */
    public void setNumLength(int numLength) {
        mNumLength = numLength;
    }

    /**
     * Theme type used for subscription background color, dark or light, default is in dark theme
     * @param themeType The theme type 0 for dark, 1 for light
     */
    public void setThemeType(int themeType) {
        mThemeType = themeType;
    }

    private void initViewElement(View view) {
        mSubNameView = (TextView) view.findViewById(R.id.sub_name);
        mSubNumView = (TextView) view.findViewById(R.id.sub_number);
        mSubShortNumView = (TextView) view.findViewById(R.id.sub_short_number);
        mSubColorView = (RelativeLayout) view.findViewById(R.id.sub_color);
    }

    /**
     * By passing subscription information into view and set views based on Subscription information.
     * Update Sub name / number / color / shortNumber views
     * @param subInfo The SubInfoRecord of subscription
     */
    public void setSubInfo(SubInfoRecord subInfo) {
        if (subInfo != null) {
            setSubIconTint(subInfo.getIconTint());
            setSubName(subInfo.getDisplayName());
            setSubNum(subInfo.getNumber());
            // TODO currently fixed to the default
            setSubShortNum(SubscriptionManager.DISPLAY_NUMBER_DEFAULT, subInfo.getNumber());
        }
    }

    /**
     * Set Sub icon tint color view
     * @param tint The icon tint color from SubInfoRecord of subscription
     */
    public void setSubIconTint(int tint) {
        mSubColorView.setBackgroundColor(tint);
    }

    /**
     * Set subscription short number view
     * @param format The format of short number of subscription, first 4 / last 4 / no display
     * @param num The subscription number
     */
    public void setSubShortNum(int format, String num) {
        String formatNum = "";
        if (!TextUtils.isEmpty(num) && format != SubscriptionManager.DISPLAY_NUMBER_NONE) {
            if (num.length() <= mNumLength) {
                formatNum = num;
            } else {
                formatNum = format == SubscriptionManager.DISPLAY_NUMBER_FIRST ?
                                      num.substring(0,mNumLength) :
                                      num.substring(num.length() - mNumLength , num.length());
            }
            mSubShortNumView.setText(formatNum);
        }
        mSubShortNumView.setVisibility(TextUtils.isEmpty(formatNum) ? View.GONE : View.VISIBLE);
    }

    /**
     * Set subscription number view
     * @param num The number of subscription
     */
    public void setSubNum(CharSequence num) {
        if (num != null) {
            mSubNumView.setText(num);
        }
        mSubNumView.setVisibility(TextUtils.isEmpty(num) ? View.GONE : View.VISIBLE);
    }

    /**
     * Set subscription name view
     * @param name The name of subscription
     */
    public void setSubName(CharSequence name) {
        if (name != null) {
            mSubNameView.setText(name);
        }
        mSubNameView.setVisibility(TextUtils.isEmpty(name) ? View.GONE : View.VISIBLE);
    }
}