summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/mediapicker/MediaChooser.java
blob: ef4067c4ef2eaf9f013296a94cb98874ac07c1e0 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (C) 2015 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.messaging.ui.mediapicker;

import android.app.FragmentManager;
import android.content.Context;
import androidx.appcompat.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.android.messaging.R;
import com.android.messaging.datamodel.binding.ImmutableBindingRef;
import com.android.messaging.datamodel.data.MediaPickerData;
import com.android.messaging.datamodel.data.DraftMessageData.DraftMessageSubscriptionDataProvider;
import com.android.messaging.ui.BasePagerViewHolder;
import com.android.messaging.util.Assert;
import com.android.messaging.util.OsUtil;

abstract class MediaChooser extends BasePagerViewHolder
        implements DraftMessageSubscriptionDataProvider {
    /** The media picker that the chooser is hosted in */
    protected final MediaPicker mMediaPicker;

    /** Referencing the main media picker binding to perform data loading */
    protected final ImmutableBindingRef<MediaPickerData> mBindingRef;

    /** True if this is the selected chooser */
    protected boolean mSelected;

    /** True if this chooser is open */
    protected boolean mOpen;

    /** The button to show in the tab strip */
    private ImageButton mTabButton;

    /** Used by subclasses to indicate that no loader is required from the data model in order for
     * this chooser to function.
     */
    public static final int NO_LOADER_REQUIRED = -1;

    /**
     * Initializes a new instance of the Chooser class
     * @param mediaPicker The media picker that the chooser is hosted in
     */
    MediaChooser(final MediaPicker mediaPicker) {
        Assert.notNull(mediaPicker);
        mMediaPicker = mediaPicker;
        mBindingRef = mediaPicker.getMediaPickerDataBinding();
        mSelected = false;
    }

    protected void setSelected(final boolean selected) {
        mSelected = selected;
        if (selected) {
            // If we're selected, it must be open
            mOpen = true;
        }
        if (mTabButton != null) {
            mTabButton.setSelected(selected);
            mTabButton.setAlpha(selected ? 1 : 0.5f);
        }
    }

    ImageButton getTabButton() {
        return mTabButton;
    }

    void onCreateTabButton(final LayoutInflater inflater, final ViewGroup parent) {
        mTabButton = (ImageButton) inflater.inflate(
                R.layout.mediapicker_tab_button,
                parent,
                false /* addToParent */);
        mTabButton.setImageResource(getIconResource());
        mTabButton.setContentDescription(
                inflater.getContext().getResources().getString(getIconDescriptionResource()));
        setSelected(mSelected);
        mTabButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                mMediaPicker.selectChooser(MediaChooser.this);
            }
        });
    }

    protected Context getContext() {
        return mMediaPicker.getActivity();
    }

    protected FragmentManager getFragmentManager() {
        return OsUtil.isAtLeastJB_MR1() ? mMediaPicker.getChildFragmentManager() :
                mMediaPicker.getFragmentManager();
    }
    protected LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(getContext());
    }

    /** Allows the chooser to handle full screen change */
    void onFullScreenChanged(final boolean fullScreen) {}

    /** Allows the chooser to handle the chooser being opened or closed */
    void onOpenedChanged(final boolean open) {
        mOpen = open;
    }

    /** @return The bit field of media types that this chooser can pick */
    public abstract int getSupportedMediaTypes();

    /** @return The resource id of the icon for the chooser */
    abstract int getIconResource();

    /** @return The resource id of the string to use for the accessibility text of the icon */
    abstract int getIconDescriptionResource();

    /**
     * Sets up the action bar to show the current state of the full-screen chooser
     * @param actionBar The action bar to populate
     */
    void updateActionBar(final ActionBar actionBar) {
        final int actionBarTitleResId = getActionBarTitleResId();
        if (actionBarTitleResId == 0) {
            actionBar.hide();
        } else {
            actionBar.setCustomView(null);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.show();
            // Use X instead of <- in the action bar
            actionBar.setHomeAsUpIndicator(R.drawable.ic_remove_small_light);
            actionBar.setTitle(actionBarTitleResId);
        }
    }

    /**
     * Returns the resource Id used for the action bar title.
     */
    abstract int getActionBarTitleResId();

    /**
     * Throws an exception if the media chooser object doesn't require data support.
     */
    public void onDataUpdated(final Object data, final int loaderId) {
        throw new IllegalStateException();
    }

    /**
     * Called by the MediaPicker to determine whether this panel can be swiped down further. If
     * not, then a swipe down gestured will be captured by the MediaPickerPanel to shrink the
     * entire panel.
     */
    public boolean canSwipeDown() {
        return false;
    }

    /**
     * Typically the media picker is closed when the IME is opened, but this allows the chooser to
     * specify that showing the IME is okay while the chooser is up
     */
    public boolean canShowIme() {
        return false;
    }

    public boolean onBackPressed() {
        return false;
    }

    public void onCreateOptionsMenu(final MenuInflater inflater, final Menu menu) {
    }

    public boolean onOptionsItemSelected(final MenuItem item) {
        return false;
    }

    public void setThemeColor(final int color) {
    }

    /**
     * Returns true if the chooser is owning any incoming touch events, so that the media picker
     * panel won't process it and slide the panel.
     */
    public boolean isHandlingTouch() {
        return false;
    }

    public void stopTouchHandling() {
    }

    @Override
    public int getConversationSelfSubId() {
        return mMediaPicker.getConversationSelfSubId();
    }

    /** Optional activity life-cycle methods to be overridden by subclasses */
    public void onPause() { }
    public void onResume() { }
    protected void onRequestPermissionsResult(
            final int requestCode, final String permissions[], final int[] grantResults) { }
}