summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/PermissionConfirmationViewHandler.java
blob: 63ed0a4538e686dd556588e57ae24f30701c2481 (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
145
146
147
148
149
package com.android.packageinstaller.permission.ui;

import android.content.Context;
import android.graphics.drawable.Icon;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.packageinstaller.R;

public abstract class PermissionConfirmationViewHandler implements
        View.OnClickListener {
    public static final int MODE_HORIZONTAL_BUTTONS = 0;
    public static final int MODE_VERTICAL_BUTTONS = 1;

    private View mRoot;
    private TextView mCurrentPageText;
    private ImageView mIcon;
    private TextView mMessage;
    private ScrollView mScrollingContainer;
    private ViewGroup mContent;
    private ViewGroup mHorizontalButtonBar;
    private ViewGroup mVerticalButtonBar;
    private Button mVerticalAllow;
    private Button mVerticalDeny;
    private Button mVerticalDenyDoNotAskAgain;
    private View mButtonBarContainer;

    private Context mContext;

    // TODO: Move these into a builder
    public abstract void onAllow();
    public abstract void onDeny();
    public abstract void onDenyDoNotAskAgain();
    public abstract CharSequence getVerticalAllowText();
    public abstract CharSequence getVerticalDenyText();
    public abstract CharSequence getVerticalDenyDoNotAskAgainText();
    public abstract CharSequence getCurrentPageText();
    public abstract Icon getPermissionIcon();
    public abstract CharSequence getMessage();

    public PermissionConfirmationViewHandler(Context context) {
        mContext = context;
    }

    public View createView() {
        mRoot = LayoutInflater.from(mContext).inflate(R.layout.grant_permissions, null);

        mMessage = (TextView) mRoot.findViewById(R.id.message);
        mCurrentPageText = (TextView) mRoot.findViewById(R.id.current_page_text);
        mIcon = (ImageView) mRoot.findViewById(R.id.icon);
        mButtonBarContainer = mRoot.findViewById(R.id.button_bar_container);
        mContent = (ViewGroup) mRoot.findViewById(R.id.content);
        mScrollingContainer = (ScrollView) mRoot.findViewById(R.id.scrolling_container);
        mHorizontalButtonBar = (ViewGroup) mRoot.findViewById(R.id.horizontal_button_bar);
        mVerticalButtonBar = (ViewGroup) mRoot.findViewById(R.id.vertical_button_bar);

        Button horizontalAllow = (Button) mRoot.findViewById(R.id.horizontal_allow_button);
        Button horizontalDeny = (Button) mRoot.findViewById(R.id.horizontal_deny_button);
        horizontalAllow.setOnClickListener(this);
        horizontalDeny.setOnClickListener(this);

        mVerticalAllow = (Button) mRoot.findViewById(R.id.vertical_allow_button);
        mVerticalDeny = (Button) mRoot.findViewById(R.id.vertical_deny_button);
        mVerticalDenyDoNotAskAgain =
                (Button) mRoot.findViewById(R.id.vertical_deny_do_not_ask_again_button);
        mVerticalAllow.setOnClickListener(this);
        mVerticalDeny.setOnClickListener(this);
        mVerticalDenyDoNotAskAgain.setOnClickListener(this);

        return mRoot;
    }

    /**
     * Child class should override this for other modes.  Call invalidate() to update the UI to the
     * new button mode.
     * @return The current mode the layout should use for the buttons
     */
    public int getButtonBarMode() {
        return MODE_HORIZONTAL_BUTTONS;
    }

    public void invalidate() {
        CharSequence currentPageText = getCurrentPageText();
        if (!TextUtils.isEmpty(currentPageText)) {
            mCurrentPageText.setText(currentPageText);
            mCurrentPageText.setVisibility(View.VISIBLE);
        } else {
            mCurrentPageText.setVisibility(View.INVISIBLE);
        }

        Icon icon = getPermissionIcon();
        if (icon != null) {
            mIcon.setImageIcon(icon);
            mIcon.setVisibility(View.VISIBLE);
        } else {
            mIcon.setVisibility(View.INVISIBLE);
        }

        mMessage.setText(getMessage());

        switch (getButtonBarMode()) {
            case MODE_HORIZONTAL_BUTTONS:
                mHorizontalButtonBar.setVisibility(View.VISIBLE);
                mVerticalButtonBar.setVisibility(View.GONE);
                break;
            case MODE_VERTICAL_BUTTONS:
                mHorizontalButtonBar.setVisibility(View.GONE);
                mVerticalButtonBar.setVisibility(View.VISIBLE);
                mVerticalAllow.setText(getVerticalAllowText());
                mVerticalDeny.setText(getVerticalDenyText());
                mVerticalDenyDoNotAskAgain.setText(getVerticalDenyDoNotAskAgainText());

                mVerticalAllow.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getDrawable(R.drawable.confirm_button), null, null, null);
                mVerticalDeny.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getDrawable(R.drawable.cancel_button), null, null, null);
                mVerticalDenyDoNotAskAgain.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getDrawable(R.drawable.cancel_button), null, null, null);
                break;
        }

        mScrollingContainer.scrollTo(0, 0);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.horizontal_allow_button:
            case R.id.vertical_allow_button:
                onAllow();
                break;
            case R.id.horizontal_deny_button:
            case R.id.vertical_deny_button:
                onDeny();
                break;
            case R.id.vertical_deny_do_not_ask_again_button:
                onDenyDoNotAskAgain();
                break;
        }
    }
}