summaryrefslogtreecommitdiffstats
path: root/src-ambient/com/android/phone/common/incall/CreditBarHelper.java
blob: 7ef75842865a1f6bcd824bc78b575b8320778670 (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
package com.android.phone.common.incall;

import android.app.PendingIntent;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.phone.common.R;

/**
 * Helper method used to handle incall credit bars
 *
 * The credit bar's origin code comes from google. We've added a second credit bar in dialer that
 * uses these same helper method.
 */
public class CreditBarHelper {

    private static final String TAG = CreditBarHelper.class.getSimpleName();

    public interface CreditBarVisibilityListener {
        public void creditsBarVisibilityChanged(int visibility);
    }

    public static void clearCallRateInformation(ViewGroup v, CreditBarVisibilityListener cpvl) {
        setCallRateInformation(null, v, null, null, null, false, cpvl);
    }

    public static void setCallRateInformation(Resources res, ViewGroup creditsBar,
            String creditText, String buttonText, final PendingIntent buttonIntent,
            boolean warnIfLow, CreditBarVisibilityListener cpvl) {
        if (TextUtils.isEmpty(creditText) && TextUtils.isEmpty(buttonText) &&
                buttonIntent == null) {
            if (creditsBar.getVisibility() != View.GONE) {
                creditsBar.setVisibility(View.GONE);
                cpvl.creditsBarVisibilityChanged(View.GONE);
            }
            return;
        }
        if (creditsBar.getVisibility() != View.VISIBLE) {
            creditsBar.setVisibility(View.VISIBLE);
            cpvl.creditsBarVisibilityChanged(View.VISIBLE);
        }

        // These views already exist, we are hijacking them.
        TextView credit = (TextView) creditsBar.findViewById(R.id.ild_country);
        TextView button = (TextView) creditsBar.findViewById(R.id.ild_rate);

        if (res != null) {
            int color = warnIfLow ?
                    res.getColor(R.color.credit_banner_alert_color) :
                    res.getColor(R.color.credit_banner_text);
            credit.setTextColor(color);
        }

        credit.setText(creditText);
        button.setText(buttonText);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (buttonIntent != null) {
                        buttonIntent.send();
                    } else {
                        Log.wtf(TAG, "The intent we attempted to fire was null");
                    }
                } catch (PendingIntent.CanceledException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void callMethodCredits(ViewGroup v, CallMethodInfo cmi, Resources r,
                                   CreditBarVisibilityListener cpvl) {
        String creditText = cmi.getCreditsDescriptionText(r);
        String buttonText = null;
        PendingIntent button;

        boolean warnIfLow = false;
        if (TextUtils.isEmpty(creditText)) {
            clearCallRateInformation(v, cpvl);
            return;
        } else {
            if (cmi.mIsAuthenticated) {
                button = cmi.mManageCreditIntent;
                if (cmi.showSubscriptions()) {
                    buttonText = cmi.mSubscriptionButtonText;
                } else {
                    if (cmi.getCurrencyAmount() <= cmi.mCreditWarn) {
                        warnIfLow = true;
                    }
                    buttonText = cmi.mCreditButtonText;
                }
            } else {
                buttonText = r.getString(R.string.sign_in_credit_banner_text);
                creditText = "";
                button = cmi.mLoginIntent;
            }
        }
        setCallRateInformation(r, v, creditText, buttonText, button, warnIfLow, cpvl);
    }
}