• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.documentsui.inspector.actions;
17 
18 import androidx.annotation.Nullable;
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.ImageButton;
25 import android.widget.ImageView;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 
29 import com.android.documentsui.R;
30 import com.android.documentsui.inspector.InspectorController;
31 
32 /**
33  * Displays different actions to the user. The action that's displayed depends on what Action is
34  * passed in to init.
35  */
36 public final class ActionView extends LinearLayout implements InspectorController.ActionDisplay {
37 
38     private Context mContext;
39     private final TextView mHeader;
40     private final ImageView mAppIcon;
41     private final TextView mAppName;
42     private final ImageButton mActionButton;
43     private Action mAction;
44 
ActionView(Context context)45     public ActionView(Context context) {
46         this(context, null);
47     }
48 
ActionView(Context context, AttributeSet attrs)49     public ActionView(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
ActionView(Context context, AttributeSet attrs, int defStyleAttr)53     public ActionView(Context context, AttributeSet attrs, int defStyleAttr) {
54         super(context, attrs, defStyleAttr);
55         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
56             Context.LAYOUT_INFLATER_SERVICE);
57         View view = inflater.inflate(R.layout.inspector_action_view, null);
58         addView(view);
59 
60         mContext = context;
61         mHeader = getSectionTitle();
62         mAppIcon = (ImageView) findViewById(R.id.app_icon);
63         mAppName = (TextView) findViewById(R.id.app_name);
64         mActionButton = (ImageButton) findViewById(R.id.inspector_action_button);
65     }
66 
getSectionTitle()67     public TextView getSectionTitle() {
68         LinearLayout header = (LinearLayout) findViewById(R.id.action_header);
69         return (TextView) header.findViewById(R.id.inspector_header_title);
70     }
71 
72     @Override
init(Action action, OnClickListener listener)73     public void init(Action action, OnClickListener listener) {
74         mAction = action;
75         setActionHeader(mAction.getHeader());
76 
77         setAppIcon(mAction.getAppIcon());
78         setAppName(mAction.getAppName());
79         mActionButton.setContentDescription(mContext.getString(action.getButtonLabel()));
80 
81         mActionButton.setOnClickListener(listener);
82         showAction(true);
83     }
84 
85     @Override
setVisible(boolean visible)86     public void setVisible(boolean visible) {
87         setVisibility(visible ? View.VISIBLE : View.GONE);
88     }
89 
90     @Override
setActionHeader(String header)91     public void setActionHeader(String header) {
92         mHeader.setText(header);
93     }
94 
95     @Override
setAppIcon(@ullable Drawable icon)96     public void setAppIcon(@Nullable Drawable icon) {
97 
98         if (icon != null) {
99             mAppIcon.setVisibility(View.VISIBLE);
100             mAppIcon.setImageDrawable(icon);
101         } else {
102             mAppIcon.setVisibility(View.GONE);
103         }
104     }
105 
106     @Override
setAppName(String name)107     public void setAppName(String name) {
108         mAppName.setText(name);
109     }
110 
111     @Override
showAction(boolean visible)112     public void showAction(boolean visible) {
113 
114         if (visible) {
115             mActionButton.setImageResource(mAction.getButtonIcon());
116             mActionButton.setVisibility(View.VISIBLE);
117         } else {
118             mActionButton.setVisibility(View.GONE);
119         }
120     }
121 }