2018-03-08 14:50:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* ThemeHelper.java is part of NewPipe
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-27 11:55:15 +00:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2020-09-07 13:25:26 +00:00
|
|
|
import android.app.Activity;
|
2017-02-27 11:55:15 +00:00
|
|
|
import android.content.Context;
|
2020-05-22 13:37:14 +00:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
2022-03-17 17:34:44 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2020-03-31 17:20:15 +00:00
|
|
|
import android.util.TypedValue;
|
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.AttrRes;
|
2022-03-17 17:34:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-09-07 13:25:26 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.StyleRes;
|
2020-09-07 13:25:26 +00:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2021-03-26 14:12:20 +00:00
|
|
|
import androidx.appcompat.app.AppCompatDelegate;
|
2022-05-16 06:55:15 +00:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources;
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2021-01-15 16:11:04 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2017-02-27 11:55:15 +00:00
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
2018-01-30 07:01:57 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2022-11-02 18:56:10 +00:00
|
|
|
import org.schabi.newpipe.info_list.ItemViewMode;
|
2017-02-27 11:55:15 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public final class ThemeHelper {
|
2020-05-22 13:37:14 +00:00
|
|
|
private ThemeHelper() {
|
|
|
|
}
|
2017-02-27 11:55:15 +00:00
|
|
|
|
2017-03-31 18:15:26 +00:00
|
|
|
/**
|
|
|
|
* Apply the selected theme (on NewPipe settings) in the context
|
2018-01-30 07:01:57 +00:00
|
|
|
* with the default style (see {@link #setTheme(Context, int)}).
|
2017-03-31 18:15:26 +00:00
|
|
|
*
|
2021-03-29 19:44:03 +00:00
|
|
|
* ThemeHelper.setDayNightMode should be called before
|
|
|
|
* the applying theme for the first time in session
|
|
|
|
*
|
2017-04-26 19:32:20 +00:00
|
|
|
* @param context context that the theme will be applied
|
2017-03-31 18:15:26 +00:00
|
|
|
*/
|
2020-03-31 17:20:15 +00:00
|
|
|
public static void setTheme(final Context context) {
|
2018-01-30 07:01:57 +00:00
|
|
|
setTheme(context, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the selected theme (on NewPipe settings) in the context,
|
|
|
|
* themed according with the styles defined for the service .
|
|
|
|
*
|
2021-03-29 19:44:03 +00:00
|
|
|
* ThemeHelper.setDayNightMode should be called before
|
|
|
|
* the applying theme for the first time in session
|
|
|
|
*
|
2018-01-30 07:01:57 +00:00
|
|
|
* @param context context that the theme will be applied
|
|
|
|
* @param serviceId the theme will be styled to the service with this id,
|
|
|
|
* pass -1 to get the default style
|
|
|
|
*/
|
2020-03-31 17:20:15 +00:00
|
|
|
public static void setTheme(final Context context, final int serviceId) {
|
2018-01-30 07:01:57 +00:00
|
|
|
context.setTheme(getThemeForService(context, serviceId));
|
2017-04-26 19:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-31 17:20:15 +00:00
|
|
|
* Return true if the selected theme (on NewPipe settings) is the Light theme.
|
2017-04-26 19:32:20 +00:00
|
|
|
*
|
|
|
|
* @param context context to get the preference
|
2020-03-31 17:20:15 +00:00
|
|
|
* @return whether the light theme is selected
|
2017-04-26 19:32:20 +00:00
|
|
|
*/
|
2020-03-31 17:20:15 +00:00
|
|
|
public static boolean isLightThemeSelected(final Context context) {
|
2021-03-18 11:35:53 +00:00
|
|
|
final String selectedThemeKey = getSelectedThemeKey(context);
|
2020-05-22 13:37:14 +00:00
|
|
|
final Resources res = context.getResources();
|
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
return selectedThemeKey.equals(res.getString(R.string.light_theme_key))
|
|
|
|
|| (selectedThemeKey.equals(res.getString(R.string.auto_device_theme_key))
|
2020-10-11 11:16:22 +00:00
|
|
|
&& !isDeviceDarkThemeEnabled(context));
|
2018-03-08 14:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a dialog theme styled according to the (default) selected theme.
|
|
|
|
*
|
|
|
|
* @param context context to get the selected theme
|
|
|
|
* @return the dialog style (the default one)
|
|
|
|
*/
|
|
|
|
@StyleRes
|
2020-03-31 17:20:15 +00:00
|
|
|
public static int getDialogTheme(final Context context) {
|
2018-03-08 14:50:46 +00:00
|
|
|
return isLightThemeSelected(context) ? R.style.LightDialogTheme : R.style.DarkDialogTheme;
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:43:54 +00:00
|
|
|
/**
|
|
|
|
* Return a min-width dialog theme styled according to the (default) selected theme.
|
|
|
|
*
|
|
|
|
* @param context context to get the selected theme
|
|
|
|
* @return the dialog style (the default one)
|
|
|
|
*/
|
|
|
|
@StyleRes
|
2020-03-31 17:20:15 +00:00
|
|
|
public static int getMinWidthDialogTheme(final Context context) {
|
|
|
|
return isLightThemeSelected(context) ? R.style.LightDialogMinWidthTheme
|
|
|
|
: R.style.DarkDialogMinWidthTheme;
|
2019-04-28 20:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
/**
|
|
|
|
* Return the selected theme styled according to the serviceId.
|
|
|
|
*
|
|
|
|
* @param context context to get the selected theme
|
|
|
|
* @param serviceId return a theme styled to this service,
|
|
|
|
* -1 to get the default
|
|
|
|
* @return the selected style (styled)
|
|
|
|
*/
|
2018-01-23 00:40:00 +00:00
|
|
|
@StyleRes
|
2020-03-31 17:20:15 +00:00
|
|
|
public static int getThemeForService(final Context context, final int serviceId) {
|
2020-05-22 13:37:14 +00:00
|
|
|
final Resources res = context.getResources();
|
2021-03-18 11:35:53 +00:00
|
|
|
final String lightThemeKey = res.getString(R.string.light_theme_key);
|
|
|
|
final String blackThemeKey = res.getString(R.string.black_theme_key);
|
|
|
|
final String automaticDeviceThemeKey = res.getString(R.string.auto_device_theme_key);
|
2018-01-23 00:40:00 +00:00
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
final String selectedThemeKey = getSelectedThemeKey(context);
|
2018-01-23 00:40:00 +00:00
|
|
|
|
2021-03-26 14:12:20 +00:00
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
int baseTheme = R.style.DarkTheme; // default to dark theme
|
|
|
|
if (selectedThemeKey.equals(lightThemeKey)) {
|
|
|
|
baseTheme = R.style.LightTheme;
|
|
|
|
} else if (selectedThemeKey.equals(blackThemeKey)) {
|
|
|
|
baseTheme = R.style.BlackTheme;
|
|
|
|
} else if (selectedThemeKey.equals(automaticDeviceThemeKey)) {
|
2020-10-11 11:16:22 +00:00
|
|
|
|
|
|
|
if (isDeviceDarkThemeEnabled(context)) {
|
2021-03-18 11:35:53 +00:00
|
|
|
// use the dark theme variant preferred by the user
|
|
|
|
final String selectedNightThemeKey = getSelectedNightThemeKey(context);
|
|
|
|
if (selectedNightThemeKey.equals(blackThemeKey)) {
|
|
|
|
baseTheme = R.style.BlackTheme;
|
2020-10-11 11:16:22 +00:00
|
|
|
} else {
|
2021-03-18 11:35:53 +00:00
|
|
|
baseTheme = R.style.DarkTheme;
|
2020-10-11 11:16:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// there is only one day theme
|
2021-03-18 11:35:53 +00:00
|
|
|
baseTheme = R.style.LightTheme;
|
2020-10-11 11:16:22 +00:00
|
|
|
}
|
2020-03-31 17:20:15 +00:00
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
|
|
|
|
if (serviceId <= -1) {
|
2021-03-18 11:35:53 +00:00
|
|
|
return baseTheme;
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final StreamingService service;
|
|
|
|
try {
|
|
|
|
service = NewPipe.getService(serviceId);
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final ExtractionException ignored) {
|
2021-03-18 11:35:53 +00:00
|
|
|
return baseTheme;
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
String themeName = "DarkTheme"; // default
|
|
|
|
if (baseTheme == R.style.LightTheme) {
|
2020-03-31 17:20:15 +00:00
|
|
|
themeName = "LightTheme";
|
2021-03-18 11:35:53 +00:00
|
|
|
} else if (baseTheme == R.style.BlackTheme) {
|
2020-03-31 17:20:15 +00:00
|
|
|
themeName = "BlackTheme";
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
|
2018-02-12 18:44:35 +00:00
|
|
|
themeName += "." + service.getServiceInfo().getName();
|
2021-03-18 11:35:53 +00:00
|
|
|
final int resourceId = context.getResources()
|
2018-12-23 21:41:42 +00:00
|
|
|
.getIdentifier(themeName, "style", context.getPackageName());
|
2018-01-30 07:01:57 +00:00
|
|
|
|
|
|
|
if (resourceId > 0) {
|
|
|
|
return resourceId;
|
|
|
|
}
|
2021-03-18 11:35:53 +00:00
|
|
|
return baseTheme;
|
2018-01-27 04:41:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 09:16:24 +00:00
|
|
|
@StyleRes
|
|
|
|
public static int getSettingsThemeStyle(final Context context) {
|
|
|
|
final Resources res = context.getResources();
|
|
|
|
final String lightTheme = res.getString(R.string.light_theme_key);
|
|
|
|
final String blackTheme = res.getString(R.string.black_theme_key);
|
|
|
|
final String automaticDeviceTheme = res.getString(R.string.auto_device_theme_key);
|
|
|
|
|
|
|
|
|
|
|
|
final String selectedTheme = getSelectedThemeKey(context);
|
|
|
|
|
|
|
|
if (selectedTheme.equals(lightTheme)) {
|
|
|
|
return R.style.LightSettingsTheme;
|
|
|
|
} else if (selectedTheme.equals(blackTheme)) {
|
|
|
|
return R.style.BlackSettingsTheme;
|
|
|
|
} else if (selectedTheme.equals(automaticDeviceTheme)) {
|
|
|
|
if (isDeviceDarkThemeEnabled(context)) {
|
|
|
|
// use the dark theme variant preferred by the user
|
|
|
|
final String selectedNightTheme = getSelectedNightThemeKey(context);
|
|
|
|
if (selectedNightTheme.equals(blackTheme)) {
|
|
|
|
return R.style.BlackSettingsTheme;
|
|
|
|
} else {
|
|
|
|
return R.style.DarkSettingsTheme;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// there is only one day theme
|
|
|
|
return R.style.LightSettingsTheme;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// default to dark theme
|
|
|
|
return R.style.DarkSettingsTheme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
/**
|
2020-03-26 00:50:32 +00:00
|
|
|
* Get a color from an attr styled according to the context's theme.
|
2020-03-31 17:20:15 +00:00
|
|
|
*
|
2020-04-07 07:17:10 +00:00
|
|
|
* @param context Android app context
|
|
|
|
* @param attrColor attribute reference of the resource
|
2020-03-31 17:20:15 +00:00
|
|
|
* @return the color
|
2018-03-08 14:50:46 +00:00
|
|
|
*/
|
2020-03-31 17:20:15 +00:00
|
|
|
public static int resolveColorFromAttr(final Context context, @AttrRes final int attrColor) {
|
2018-03-08 14:50:46 +00:00
|
|
|
final TypedValue value = new TypedValue();
|
|
|
|
context.getTheme().resolveAttribute(attrColor, value, true);
|
|
|
|
|
|
|
|
if (value.resourceId != 0) {
|
|
|
|
return ContextCompat.getColor(context, value.resourceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.data;
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:34:44 +00:00
|
|
|
/**
|
|
|
|
* Resolves a {@link Drawable} by it's id.
|
|
|
|
*
|
|
|
|
* @param context Context
|
|
|
|
* @param attrResId Resource id
|
|
|
|
* @return the {@link Drawable}
|
|
|
|
*/
|
2022-05-16 06:55:15 +00:00
|
|
|
public static Drawable resolveDrawable(@NonNull final Context context,
|
|
|
|
@AttrRes final int attrResId) {
|
2022-03-17 17:34:44 +00:00
|
|
|
final TypedValue typedValue = new TypedValue();
|
|
|
|
context.getTheme().resolveAttribute(attrResId, typedValue, true);
|
2022-05-16 06:55:15 +00:00
|
|
|
return AppCompatResources.getDrawable(context, typedValue.resourceId);
|
2022-03-17 17:34:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:59:00 +00:00
|
|
|
/**
|
|
|
|
* Gets a runtime dimen from the {@code android} package. Should be used for dimens for which
|
|
|
|
* normal accessing with {@code R.dimen.} is not available.
|
|
|
|
*
|
|
|
|
* @param context context
|
|
|
|
* @param name dimen resource name (e.g. navigation_bar_height)
|
|
|
|
* @return the obtained dimension, in pixels, or 0 if the resource could not be resolved
|
|
|
|
*/
|
|
|
|
public static int getAndroidDimenPx(@NonNull final Context context, final String name) {
|
|
|
|
final int resId = context.getResources().getIdentifier(name, "dimen", "android");
|
|
|
|
if (resId <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return context.getResources().getDimensionPixelSize(resId);
|
|
|
|
}
|
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
private static String getSelectedThemeKey(final Context context) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final String themeKey = context.getString(R.string.theme_key);
|
|
|
|
final String defaultTheme = context.getResources().getString(R.string.default_theme_value);
|
2020-03-31 17:20:15 +00:00
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getString(themeKey, defaultTheme);
|
2018-03-08 14:50:46 +00:00
|
|
|
}
|
2020-09-07 13:25:26 +00:00
|
|
|
|
2021-03-18 11:35:53 +00:00
|
|
|
private static String getSelectedNightThemeKey(final Context context) {
|
2020-10-11 11:16:22 +00:00
|
|
|
final String nightThemeKey = context.getString(R.string.night_theme_key);
|
|
|
|
final String defaultNightTheme = context.getResources()
|
|
|
|
.getString(R.string.default_night_theme_value);
|
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getString(nightThemeKey, defaultNightTheme);
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:25:26 +00:00
|
|
|
/**
|
|
|
|
* Sets the title to the activity, if the activity is an {@link AppCompatActivity} and has an
|
|
|
|
* action bar.
|
2020-05-22 13:37:14 +00:00
|
|
|
*
|
2020-09-07 13:25:26 +00:00
|
|
|
* @param activity the activity to set the title of
|
2020-05-22 13:37:14 +00:00
|
|
|
* @param title the title to set to the activity
|
2020-09-07 13:25:26 +00:00
|
|
|
*/
|
|
|
|
public static void setTitleToAppCompatActivity(@Nullable final Activity activity,
|
|
|
|
final CharSequence title) {
|
|
|
|
if (activity instanceof AppCompatActivity) {
|
|
|
|
final ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 13:37:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the device theme
|
|
|
|
* <p>
|
|
|
|
* It will return true if the device 's theme is dark, false otherwise.
|
|
|
|
* <p>
|
|
|
|
* From https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#java
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @return true:dark theme, false:light or unknown
|
|
|
|
*/
|
2020-07-03 16:44:34 +00:00
|
|
|
public static boolean isDeviceDarkThemeEnabled(final Context context) {
|
|
|
|
final int deviceTheme = context.getResources().getConfiguration().uiMode
|
2020-05-22 13:37:14 +00:00
|
|
|
& Configuration.UI_MODE_NIGHT_MASK;
|
|
|
|
switch (deviceTheme) {
|
|
|
|
case Configuration.UI_MODE_NIGHT_YES:
|
|
|
|
return true;
|
|
|
|
case Configuration.UI_MODE_NIGHT_UNDEFINED:
|
|
|
|
case Configuration.UI_MODE_NIGHT_NO:
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 14:12:20 +00:00
|
|
|
|
|
|
|
public static void setDayNightMode(final Context context) {
|
|
|
|
setDayNightMode(context, ThemeHelper.getSelectedThemeKey(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setDayNightMode(final Context context, final String selectedThemeKey) {
|
|
|
|
final Resources res = context.getResources();
|
|
|
|
|
|
|
|
if (selectedThemeKey.equals(res.getString(R.string.light_theme_key))) {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
|
|
|
} else if (selectedThemeKey.equals(res.getString(R.string.dark_theme_key))
|
|
|
|
|| selectedThemeKey.equals(res.getString(R.string.black_theme_key))) {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
|
|
|
} else {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 18:47:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the grid layout or the list layout should be used. If the user set "auto"
|
|
|
|
* mode in settings, decides based on screen orientation (landscape) and size.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @return true:use grid layout, false:use list layout
|
|
|
|
*/
|
|
|
|
public static boolean shouldUseGridLayout(final Context context) {
|
2022-11-02 18:56:10 +00:00
|
|
|
final ItemViewMode mode = getItemViewMode(context);
|
|
|
|
return mode == ItemViewMode.GRID;
|
2021-07-19 18:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-31 08:51:59 +00:00
|
|
|
* Calculates the number of grid channel info items that can fit horizontally on the screen.
|
2021-07-19 18:47:50 +00:00
|
|
|
*
|
|
|
|
* @param context the context to use
|
2021-07-31 08:51:59 +00:00
|
|
|
* @return the span count of grid channel info items
|
|
|
|
*/
|
|
|
|
public static int getGridSpanCountChannels(final Context context) {
|
|
|
|
return getGridSpanCount(context,
|
|
|
|
context.getResources().getDimensionPixelSize(R.dimen.channel_item_grid_min_width));
|
|
|
|
}
|
|
|
|
|
2022-11-02 18:56:10 +00:00
|
|
|
/**
|
|
|
|
* Returns item view mode.
|
|
|
|
* @param context to read preference and parse string
|
|
|
|
* @return Returns one of ItemViewMode
|
|
|
|
*/
|
|
|
|
public static ItemViewMode getItemViewMode(final Context context) {
|
|
|
|
final String listMode = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getString(context.getString(R.string.list_view_mode_key),
|
|
|
|
context.getString(R.string.list_view_mode_value));
|
|
|
|
final ItemViewMode result;
|
|
|
|
if (listMode.equals(context.getString(R.string.list_view_mode_list_key))) {
|
|
|
|
result = ItemViewMode.LIST;
|
|
|
|
} else if (listMode.equals(context.getString(R.string.list_view_mode_grid_key))) {
|
|
|
|
result = ItemViewMode.GRID;
|
|
|
|
} else if (listMode.equals(context.getString(R.string.list_view_mode_card_key))) {
|
|
|
|
result = ItemViewMode.CARD;
|
|
|
|
} else {
|
|
|
|
// Auto mode - evaluate whether to use Grid based on screen real estate.
|
|
|
|
final Configuration configuration = context.getResources().getConfiguration();
|
|
|
|
final boolean useGrid = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
|
|
|
|
&& configuration.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
|
|
|
|
if (useGrid) {
|
|
|
|
result = ItemViewMode.GRID;
|
|
|
|
} else {
|
|
|
|
result = ItemViewMode.LIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-31 08:51:59 +00:00
|
|
|
/**
|
|
|
|
* Calculates the number of grid stream info items that can fit horizontally on the screen. The
|
|
|
|
* width of a grid stream info item is obtained from the thumbnail width plus the right and left
|
|
|
|
* paddings.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @return the span count of grid stream info items
|
2021-07-19 18:47:50 +00:00
|
|
|
*/
|
2021-07-31 08:51:59 +00:00
|
|
|
public static int getGridSpanCountStreams(final Context context) {
|
2021-07-19 18:47:50 +00:00
|
|
|
final Resources res = context.getResources();
|
2021-07-31 08:51:59 +00:00
|
|
|
return getGridSpanCount(context,
|
|
|
|
res.getDimensionPixelSize(R.dimen.video_item_grid_thumbnail_image_width)
|
|
|
|
+ res.getDimensionPixelSize(R.dimen.video_item_search_padding) * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the number of grid items that can fit horizontally on the screen based on the
|
|
|
|
* minimum width.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param minWidth the minimum width of items in the grid
|
|
|
|
* @return the span count of grid list items
|
|
|
|
*/
|
|
|
|
public static int getGridSpanCount(final Context context, final int minWidth) {
|
|
|
|
return Math.max(1, context.getResources().getDisplayMetrics().widthPixels / minWidth);
|
2021-07-19 18:47:50 +00:00
|
|
|
}
|
2017-02-27 11:55:15 +00:00
|
|
|
}
|