1package com.android.packageinstaller.permission.ui.television;
2
3import android.content.Context;
4import android.graphics.PixelFormat;
5import android.graphics.drawable.Icon;
6import android.os.Bundle;
7import android.view.Gravity;
8import android.view.LayoutInflater;
9import android.view.View;
10import android.view.View.OnClickListener;
11import android.view.WindowManager;
12import android.widget.Button;
13import android.widget.ImageView;
14import android.widget.LinearLayout;
15import android.widget.TextView;
16
17import com.android.packageinstaller.R;
18import com.android.packageinstaller.permission.ui.GrantPermissionsViewHandler;
19
20/**
21 * TV-specific view handler for the grant permissions activity.
22 */
23public final class GrantPermissionsViewHandlerImpl implements GrantPermissionsViewHandler, OnClickListener {
24
25    private static final String ARG_GROUP_NAME = "ARG_GROUP_NAME";
26
27    private final Context mContext;
28
29    private ResultListener mResultListener;
30
31    private String mGroupName;
32
33    private LinearLayout mRootView;
34    private TextView mMessageView;
35    private ImageView mIconView;
36    private TextView mCurrentGroupView;
37    private Button mAllowButton;
38    private Button mSoftDenyButton;
39    private Button mHardDenyButton;
40
41    public GrantPermissionsViewHandlerImpl(Context context, String appPackageName) {
42        mContext = context;
43    }
44
45    @Override
46    public GrantPermissionsViewHandlerImpl setResultListener(ResultListener listener) {
47        mResultListener = listener;
48        return this;
49    }
50
51    @Override
52    public View createView() {
53        mRootView = (LinearLayout) LayoutInflater.from(mContext)
54                .inflate(R.layout.grant_permissions, null);
55
56        mMessageView = (TextView) mRootView.findViewById(R.id.permission_message);
57        mIconView = (ImageView) mRootView.findViewById(R.id.permission_icon);
58        mCurrentGroupView = (TextView) mRootView.findViewById(R.id.current_page_text);
59        mAllowButton = (Button) mRootView.findViewById(R.id.permission_allow_button);
60        mSoftDenyButton = (Button) mRootView.findViewById(R.id.permission_deny_button);
61        mHardDenyButton = (Button) mRootView.findViewById(
62                R.id.permission_deny_dont_ask_again_button);
63
64        mAllowButton.setOnClickListener(this);
65        mSoftDenyButton.setOnClickListener(this);
66        mHardDenyButton.setOnClickListener(this);
67
68        return mRootView;
69    }
70
71    @Override
72    public void updateWindowAttributes(WindowManager.LayoutParams outLayoutParams) {
73        outLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
74        outLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
75        outLayoutParams.format = PixelFormat.OPAQUE;
76        outLayoutParams.gravity = Gravity.BOTTOM;
77        outLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
78        outLayoutParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
79    }
80
81    @Override
82    public void updateUi(String groupName, int groupCount, int groupIndex, Icon icon,
83            CharSequence message, boolean showDoNotAsk) {
84        mGroupName = groupName;
85
86        mMessageView.setText(message);
87        mIconView.setImageIcon(icon);
88        mHardDenyButton.setVisibility(showDoNotAsk ? View.VISIBLE : View.GONE);
89        if (groupCount > 1) {
90            mCurrentGroupView.setVisibility(View.VISIBLE);
91            mCurrentGroupView.setText(mContext.getString(R.string.current_permission_template,
92                    groupIndex + 1, groupCount));
93        } else {
94            mCurrentGroupView.setVisibility(View.INVISIBLE);
95        }
96    }
97
98    @Override
99    public void saveInstanceState(Bundle outState) {
100        outState.putString(ARG_GROUP_NAME, mGroupName);
101    }
102
103    @Override
104    public void loadInstanceState(Bundle savedInstanceState) {
105        mGroupName = savedInstanceState.getString(ARG_GROUP_NAME);
106    }
107
108    @Override
109    public void onClick(View view) {
110        boolean granted = false;
111        boolean doNotAskAgain = false;
112        switch (view.getId()) {
113            case R.id.permission_allow_button:
114                granted = true;
115                break;
116            case R.id.permission_deny_dont_ask_again_button:
117                doNotAskAgain = true;
118                break;
119        }
120        if (mResultListener != null) {
121            mResultListener.onPermissionGrantResult(mGroupName, granted, doNotAskAgain);
122        }
123    }
124
125    @Override
126    public void onBackPressed() {
127        if (mResultListener != null) {
128            mResultListener.onPermissionGrantResult(mGroupName, false, false);
129        }
130    }
131}
132