forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbarActivity.java
More file actions
344 lines (296 loc) · 12.4 KB
/
ToolbarActivity.java
File metadata and controls
344 lines (296 loc) · 12.4 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2023 ZetaTom
* SPDX-FileCopyrightText: 2023 Parneet Singh <gurayaparneet@gmail.com>
* SPDX-FileCopyrightText: 2022 Brey Álvaro Brey <alvaro@alvarobrey.com>
* SPDX-FileCopyrightText: 2022 TSI-mc
* SPDX-FileCopyrightText: 2020 Joris Bodin <joris.bodin@infomaniak.com>
* SPDX-FileCopyrightText: 2016-2022 Andy Scherzinger
* SPDX-FileCopyrightText: 2018-2022 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-FileCopyrightText: 2016 Nextcloud
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.owncloud.android.ui.activity;
import android.animation.AnimatorInflater;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.textview.MaterialTextView;
import com.nextcloud.android.common.ui.theme.utils.ColorRole;
import com.nextcloud.client.di.Injectable;
import com.owncloud.android.R;
import com.nmc.android.appupdate.InAppUpdateHelper;
import com.nmc.android.appupdate.InAppUpdateHelperImpl;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.utils.theme.ThemeColorUtils;
import com.owncloud.android.utils.theme.ThemeUtils;
import com.owncloud.android.utils.theme.ViewThemeUtils;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.content.ContextCompat;
/**
* Base class providing toolbar registration functionality, see {@link #setupToolbar(boolean, boolean)}.
*/
public abstract class ToolbarActivity extends BaseActivity implements Injectable {
protected MaterialButton mMenuButton;
protected MaterialTextView mSearchText;
protected MaterialButton mSwitchAccountButton;
protected MaterialButton mNotificationButton;
private AppBarLayout mAppBar;
private RelativeLayout mDefaultToolbar;
private MaterialToolbar mToolbar;
private MaterialCardView mHomeSearchToolbar;
private ImageView mPreviewImage;
private FrameLayout mPreviewImageContainer;
private LinearLayout mInfoBox;
private TextView mInfoBoxMessage;
protected AppCompatSpinner mToolbarSpinner;
private boolean isHomeSearchToolbarShow = false;
@Inject public ThemeColorUtils themeColorUtils;
@Inject public ThemeUtils themeUtils;
@Inject public ViewThemeUtils viewThemeUtils;
private InAppUpdateHelper inAppUpdateHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inAppUpdateHelper = new InAppUpdateHelperImpl(this);
}
/**
* Toolbar setup that must be called in implementer's {@link #onCreate} after {@link #setContentView} if they want
* to use the toolbar.
*/
private void setupToolbar(boolean isHomeSearchToolbarShow, boolean showSortListButtonGroup) {
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mAppBar = findViewById(R.id.appbar);
mDefaultToolbar = findViewById(R.id.default_toolbar);
mHomeSearchToolbar = findViewById(R.id.home_toolbar);
mMenuButton = findViewById(R.id.menu_button);
mSearchText = findViewById(R.id.search_text);
mSwitchAccountButton = findViewById(R.id.switch_account_button);
mNotificationButton = findViewById(R.id.notification_button);
if (showSortListButtonGroup) {
findViewById(R.id.sort_list_button_group).setVisibility(View.VISIBLE);
}
this.isHomeSearchToolbarShow = isHomeSearchToolbarShow;
updateActionBarTitleAndHomeButton(null);
mInfoBox = findViewById(R.id.info_box);
mInfoBoxMessage = findViewById(R.id.info_box_message);
mPreviewImage = findViewById(R.id.preview_image);
mPreviewImageContainer = findViewById(R.id.preview_image_frame);
mToolbarSpinner = findViewById(R.id.toolbar_spinner);
viewThemeUtils.material.themeToolbar(mToolbar);
viewThemeUtils.material.colorToolbarOverflowIcon(mToolbar);
viewThemeUtils.platform.themeStatusBar(this);
viewThemeUtils.material.colorMaterialTextButton(mSwitchAccountButton);
viewThemeUtils.material.themeSearchCardView(mHomeSearchToolbar);
viewThemeUtils.material.colorMaterialButtonContent(mMenuButton, ColorRole.ON_SURFACE);
viewThemeUtils.material.colorMaterialButtonContent(mNotificationButton, ColorRole.ON_SURFACE);
viewThemeUtils.platform.colorTextView(mSearchText, ColorRole.ON_SURFACE_VARIANT);
}
public void setupToolbarShowOnlyMenuButtonAndTitle(String title, View.OnClickListener toggleDrawer) {
setupToolbar(false, false);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
}
LinearLayout toolbar = findViewById(R.id.toolbar_linear_layout);
MaterialButton menuButton = findViewById(R.id.toolbar_menu_button);
MaterialTextView titleTextView = findViewById(R.id.toolbar_title);
titleTextView.setText(title);
titleTextView.setTextColor(ContextCompat.getColor(this, R.color.foreground_highlight));
menuButton.setIconTint(ContextCompat.getColorStateList(this, R.color.foreground_highlight));
toolbar.setVisibility(View.VISIBLE);
menuButton.setOnClickListener(toggleDrawer);
}
public void setupToolbar() {
setupToolbar(false, false);
}
public void setupHomeSearchToolbarWithSortAndListButtons() {
setupToolbar(true, true);
}
/**
* Updates title bar and home buttons (state and icon).
*/
protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
String title;
boolean isRoot = isRoot(chosenFile);
title = isRoot ? themeUtils.getDefaultDisplayNameForRootFolder(this) : fileDataStorageManager.getFilenameConsideringOfflineOperation(chosenFile);
updateActionBarTitleAndHomeButtonByString(title);
if (mAppBar != null) {
showHomeSearchToolbar(title, isRoot);
}
}
public void showSearchView() {
if (isHomeSearchToolbarShow) {
showHomeSearchToolbar(false);
}
}
public void hideSearchView(OCFile chosenFile) {
if (isHomeSearchToolbarShow) {
showHomeSearchToolbar(isRoot(chosenFile));
}
}
private void showHomeSearchToolbar(String title, boolean isRoot) {
showHomeSearchToolbar(isHomeSearchToolbarShow && isRoot);
mSearchText.setText(getString(R.string.appbar_search_in, title));
}
@SuppressLint("PrivateResource")
private void showHomeSearchToolbar(boolean isShow) {
viewThemeUtils.material.themeToolbar(mToolbar);
if (isShow) {
viewThemeUtils.platform.resetStatusBar(this);
mAppBar.setStateListAnimator(AnimatorInflater.loadStateListAnimator(mAppBar.getContext(),
R.animator.appbar_elevation_off));
mDefaultToolbar.setVisibility(View.GONE);
mHomeSearchToolbar.setVisibility(View.VISIBLE);
viewThemeUtils.material.themeCardView(mHomeSearchToolbar);
viewThemeUtils.material.themeSearchBarText(mSearchText);
} else {
mAppBar.setStateListAnimator(AnimatorInflater.loadStateListAnimator(mAppBar.getContext(),
R.animator.appbar_elevation_on));
viewThemeUtils.platform.themeStatusBar(this);
mDefaultToolbar.setVisibility(View.VISIBLE);
mHomeSearchToolbar.setVisibility(View.GONE);
}
}
/**
* Updates title bar and home buttons (state and icon).
*/
public void updateActionBarTitleAndHomeButtonByString(String title) {
// set & color the chosen title
ActionBar actionBar = getSupportActionBar();
// set home button properties
if (actionBar != null) {
if (title != null) {
actionBar.setTitle(title);
actionBar.setDisplayShowTitleEnabled(true);
} else {
actionBar.setDisplayShowTitleEnabled(false);
}
}
}
/**
* checks if the given file is the root folder.
*
* @param file file to be checked if it is the root folder
* @return <code>true</code> if it is <code>null</code> or the root folder, else returns <code>false</code>
*/
public boolean isRoot(OCFile file) {
return file == null || (file.isFolder() && file.getParentId() == FileDataStorageManager.ROOT_PARENT_ID);
}
/**
* shows the toolbar's info box with the given text.
*
* @param text the text to be displayed
*/
protected final void showInfoBox(@StringRes int text) {
if (mInfoBox != null && mInfoBoxMessage != null) {
mInfoBox.setVisibility(View.VISIBLE);
mInfoBoxMessage.setText(text);
}
}
/**
* Hides the toolbar's info box.
*/
public final void hideInfoBox() {
if (mInfoBox != null) {
mInfoBox.setVisibility(View.GONE);
}
}
public void setPreviewImageVisibility(boolean isVisibility) {
if (mPreviewImage != null && mPreviewImageContainer != null) {
if (isVisibility) {
mToolbar.setTitle(null);
mToolbar.setBackgroundColor(Color.TRANSPARENT);
} else {
mToolbar.setBackgroundResource(R.color.appbar);
}
mPreviewImageContainer.setVisibility(isVisibility ? View.VISIBLE : View.GONE);
}
}
public void hidePreviewImage() {
setPreviewImageVisibility(false);
}
public void showSortListGroup(boolean show) {
findViewById(R.id.sort_list_button_group).setVisibility(show ? View.VISIBLE : View.GONE);
}
public boolean sortListGroupVisibility(){
return findViewById(R.id.sort_list_button_group).getVisibility() == View.VISIBLE;
}
/**
* Change the bitmap for the toolbar's preview image.
*
* @param bitmap bitmap of the preview image
*/
public void setPreviewImageBitmap(Bitmap bitmap) {
if (mPreviewImage != null) {
mPreviewImage.setImageBitmap(bitmap);
setPreviewImageVisibility(true);
}
}
/**
* Change the drawable for the toolbar's preview image.
*
* @param drawable drawable of the preview image
*/
public void setPreviewImageDrawable(Drawable drawable) {
if (mPreviewImage != null) {
mPreviewImage.setImageDrawable(drawable);
setPreviewImageVisibility(true);
}
}
/**
* get the toolbar's preview image view.
*/
public ImageView getPreviewImageView() {
return mPreviewImage;
}
public FrameLayout getPreviewImageContainer() {
return mPreviewImageContainer;
}
public void updateToolbarSubtitle(@NonNull String subtitle) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSubtitle(subtitle);
viewThemeUtils.androidx.themeActionBarSubtitle(this, actionBar);
}
}
public void clearToolbarSubtitle() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSubtitle(null);
}
}
@Override
protected void onResume() {
super.onResume();
// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all entry points into the app.
inAppUpdateHelper.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
inAppUpdateHelper.onDestroy();
inAppUpdateHelper = null;
}
}