Implement UI for subscriptions import/export
- Nice and easy to use import/export options in the subscriptions fragment - Includes instructions for each service (in the import fragment/screen)
This commit is contained in:
parent
83b084a90b
commit
cc2feab37e
52 changed files with 1329 additions and 127 deletions
|
|
@ -1,3 +1,22 @@
|
|||
/*
|
||||
* Copyright 2018 Mauricio Colli <mauriciocolli@outlook.com>
|
||||
* AnimationUtils.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/>.
|
||||
*/
|
||||
|
||||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.animation.Animator;
|
||||
|
|
@ -19,7 +38,9 @@ public class AnimationUtils {
|
|||
private static final boolean DEBUG = MainActivity.DEBUG;
|
||||
|
||||
public enum Type {
|
||||
ALPHA, SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA, SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA
|
||||
ALPHA,
|
||||
SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA,
|
||||
SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA
|
||||
}
|
||||
|
||||
public static void animateView(View view, boolean enterOrExit, long duration) {
|
||||
|
|
@ -168,6 +189,58 @@ public class AnimationUtils {
|
|||
viewPropertyAnimator.start();
|
||||
}
|
||||
|
||||
public static ValueAnimator animateHeight(final View view, long duration, int targetHeight) {
|
||||
final int height = view.getHeight();
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "animateHeight: duration = [" + duration + "], from " + height + " to → " + targetHeight + " in: " + view);
|
||||
}
|
||||
|
||||
ValueAnimator animator = ValueAnimator.ofFloat(height, targetHeight);
|
||||
animator.setInterpolator(new FastOutSlowInInterpolator());
|
||||
animator.setDuration(duration);
|
||||
animator.addUpdateListener(animation -> {
|
||||
final float value = (float) animation.getAnimatedValue();
|
||||
view.getLayoutParams().height = (int) value;
|
||||
view.requestLayout();
|
||||
});
|
||||
animator.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
view.getLayoutParams().height = targetHeight;
|
||||
view.requestLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationCancel(Animator animation) {
|
||||
view.getLayoutParams().height = targetHeight;
|
||||
view.requestLayout();
|
||||
}
|
||||
});
|
||||
animator.start();
|
||||
|
||||
return animator;
|
||||
}
|
||||
|
||||
public static void animateRotation(final View view, long duration, int targetRotation) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "animateRotation: duration = [" + duration + "], from " + view.getRotation() + " to → " + targetRotation + " in: " + view);
|
||||
}
|
||||
view.animate().setListener(null).cancel();
|
||||
|
||||
view.animate().rotation(targetRotation).setDuration(duration).setInterpolator(new FastOutSlowInInterpolator())
|
||||
.setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationCancel(Animator animation) {
|
||||
view.setRotation(targetRotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
view.setRotation(targetRotation);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Internals
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
|
|
|||
|
|
@ -1,10 +1,32 @@
|
|||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.TypedValue;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
|
||||
import com.nononsenseapps.filepicker.FilePickerFragment;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.FilePickerActivity {
|
||||
|
||||
private CustomFilePickerFragment currentFragment;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
if(ThemeHelper.isLightThemeSelected(this)) {
|
||||
|
|
@ -14,4 +36,98 @@ public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.File
|
|||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
// If at top most level, normal behaviour
|
||||
if (currentFragment.isBackTop()) {
|
||||
super.onBackPressed();
|
||||
} else {
|
||||
// Else go up
|
||||
currentFragment.goUp();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractFilePickerFragment<File> getFragment(@Nullable String startPath, int mode, boolean allowMultiple, boolean allowCreateDir, boolean allowExistingFile, boolean singleClick) {
|
||||
final CustomFilePickerFragment fragment = new CustomFilePickerFragment();
|
||||
fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
|
||||
mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
|
||||
return currentFragment = fragment;
|
||||
}
|
||||
|
||||
public static Intent chooseSingleFile(@NonNull Context context) {
|
||||
return new Intent(context, FilePickerActivityHelper.class)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_SINGLE_CLICK, true)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE);
|
||||
}
|
||||
|
||||
public static Intent chooseFileToSave(@NonNull Context context, @Nullable String startPath) {
|
||||
return new Intent(context, FilePickerActivityHelper.class)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_EXISTING_FILE, true)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, startPath)
|
||||
.putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_NEW_FILE);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Internal
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
public static class CustomFilePickerFragment extends FilePickerFragment {
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return super.onCreateView(inflater, container, savedInstanceState);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
final RecyclerView.ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
|
||||
|
||||
final View view = viewHolder.itemView.findViewById(android.R.id.text1);
|
||||
if (view instanceof TextView) {
|
||||
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.file_picker_items_text_size));
|
||||
}
|
||||
|
||||
return viewHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClickOk(@NonNull View view) {
|
||||
if (mode == MODE_NEW_FILE && getNewFileName().isEmpty()) {
|
||||
if (mToast != null) mToast.cancel();
|
||||
mToast = Toast.makeText(getActivity(), R.string.file_name_empty_error, Toast.LENGTH_SHORT);
|
||||
mToast.show();
|
||||
return;
|
||||
}
|
||||
|
||||
super.onClickOk(view);
|
||||
}
|
||||
|
||||
public File getBackTop() {
|
||||
if (getArguments() == null) return Environment.getExternalStorageDirectory();
|
||||
|
||||
final String path = getArguments().getString(KEY_START_PATH, "/");
|
||||
if (path.contains(Environment.getExternalStorageDirectory().getPath())) {
|
||||
return Environment.getExternalStorageDirectory();
|
||||
}
|
||||
|
||||
return getPath(path);
|
||||
}
|
||||
|
||||
public boolean isBackTop() {
|
||||
return compareFiles(mCurrentPath, getBackTop()) == 0 || compareFiles(mCurrentPath, new File("/")) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<SortedList<File>> loader, SortedList<File> data) {
|
||||
super.onLoadFinished(loader, data);
|
||||
layoutManager.scrollToPosition(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
|
|
@ -11,6 +12,7 @@ import android.support.annotation.NonNull;
|
|||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
|
@ -38,6 +40,7 @@ import org.schabi.newpipe.fragments.list.search.SearchFragment;
|
|||
import org.schabi.newpipe.fragments.local.bookmark.LastPlayedFragment;
|
||||
import org.schabi.newpipe.fragments.local.bookmark.LocalPlaylistFragment;
|
||||
import org.schabi.newpipe.fragments.local.bookmark.MostPlayedFragment;
|
||||
import org.schabi.newpipe.fragments.subscription.SubscriptionsImportFragment;
|
||||
import org.schabi.newpipe.history.HistoryActivity;
|
||||
import org.schabi.newpipe.player.BackgroundPlayer;
|
||||
import org.schabi.newpipe.player.BackgroundPlayerActivity;
|
||||
|
|
@ -247,6 +250,12 @@ public class NavigationHelper {
|
|||
// Through FragmentManager
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
@SuppressLint("CommitTransaction")
|
||||
private static FragmentTransaction defaultTransaction(FragmentManager fragmentManager) {
|
||||
return fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out);
|
||||
}
|
||||
|
||||
public static void gotoMainFragment(FragmentManager fragmentManager) {
|
||||
ImageLoader.getInstance().clearMemoryCache();
|
||||
|
||||
|
|
@ -258,8 +267,7 @@ public class NavigationHelper {
|
|||
InfoCache.getInstance().trimCache();
|
||||
|
||||
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, new MainFragment())
|
||||
.addToBackStack(MAIN_FRAGMENT_TAG)
|
||||
.commit();
|
||||
|
|
@ -276,8 +284,7 @@ public class NavigationHelper {
|
|||
}
|
||||
|
||||
public static void openSearchFragment(FragmentManager fragmentManager, int serviceId, String query) {
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, SearchFragment.getInstance(serviceId, query))
|
||||
.addToBackStack(SEARCH_FRAGMENT_TAG)
|
||||
.commit();
|
||||
|
|
@ -301,8 +308,7 @@ public class NavigationHelper {
|
|||
VideoDetailFragment instance = VideoDetailFragment.getInstance(serviceId, url, title);
|
||||
instance.setAutoplay(autoPlay);
|
||||
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, instance)
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
|
|
@ -310,8 +316,7 @@ public class NavigationHelper {
|
|||
|
||||
public static void openChannelFragment(FragmentManager fragmentManager, int serviceId, String url, String name) {
|
||||
if (name == null) name = "";
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, ChannelFragment.getInstance(serviceId, url, name))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
|
|
@ -319,25 +324,21 @@ public class NavigationHelper {
|
|||
|
||||
public static void openPlaylistFragment(FragmentManager fragmentManager, int serviceId, String url, String name) {
|
||||
if (name == null) name = "";
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, PlaylistFragment.getInstance(serviceId, url, name))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public static void openWhatsNewFragment(FragmentManager fragmentManager) {
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, new FeedFragment())
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public static void openKioskFragment(FragmentManager fragmentManager, int serviceId, String kioskId)
|
||||
throws ExtractionException {
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
public static void openKioskFragment(FragmentManager fragmentManager, int serviceId, String kioskId) throws ExtractionException {
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, KioskFragment.getInstance(serviceId, kioskId))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
|
|
@ -345,28 +346,33 @@ public class NavigationHelper {
|
|||
|
||||
public static void openLocalPlaylistFragment(FragmentManager fragmentManager, long playlistId, String name) {
|
||||
if (name == null) name = "";
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, LocalPlaylistFragment.getInstance(playlistId, name))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public static void openLastPlayedFragment(FragmentManager fragmentManager) {
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, new LastPlayedFragment())
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public static void openMostPlayedFragment(FragmentManager fragmentManager) {
|
||||
fragmentManager.beginTransaction()
|
||||
.setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, R.animator.custom_fade_in, R.animator.custom_fade_out)
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, new MostPlayedFragment())
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public static void openSubscriptionsImportFragment(FragmentManager fragmentManager, int serviceId) {
|
||||
defaultTransaction(fragmentManager)
|
||||
.replace(R.id.fragment_holder, SubscriptionsImportFragment.getInstance(serviceId))
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Through Intents
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package org.schabi.newpipe.util;
|
|||
import android.content.Context;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import org.schabi.newpipe.BuildConfig;
|
||||
import org.schabi.newpipe.R;
|
||||
|
|
@ -26,6 +27,39 @@ public class ServiceHelper {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource string with instructions for importing subscriptions for each service.
|
||||
*
|
||||
* @return the string resource containing the instructions or -1 if the service don't support it
|
||||
*/
|
||||
@StringRes
|
||||
public static int getImportInstructions(int serviceId) {
|
||||
switch (serviceId) {
|
||||
case 0:
|
||||
return R.string.import_youtube_instructions;
|
||||
case 1:
|
||||
return R.string.import_soundcloud_instructions;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For services that support importing from a channel url, return a hint that will
|
||||
* be used in the EditText that the user will type in his channel url.
|
||||
*
|
||||
* @return the hint's string resource or -1 if the service don't support it
|
||||
*/
|
||||
@StringRes
|
||||
public static int getImportInstructionsHint(int serviceId) {
|
||||
switch (serviceId) {
|
||||
case 1:
|
||||
return R.string.import_soundcloud_instructions_hint;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getSelectedServiceId(Context context) {
|
||||
if (BuildConfig.BUILD_TYPE.equals("release")) return DEFAULT_FALLBACK_SERVICE.getServiceId();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,22 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.content.Context;
|
||||
|
|
@ -5,6 +24,9 @@ import android.content.res.TypedArray;
|
|||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.AttrRes;
|
||||
import android.support.annotation.StyleRes;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.TypedValue;
|
||||
import android.view.ContextThemeWrapper;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.NewPipe;
|
||||
|
|
@ -41,16 +63,57 @@ public class ThemeHelper {
|
|||
* @param context context to get the preference
|
||||
*/
|
||||
public static boolean isLightThemeSelected(Context context) {
|
||||
return getSelectedTheme(context).equals(context.getResources().getString(R.string.light_theme_key));
|
||||
return getSelectedThemeString(context).equals(context.getResources().getString(R.string.light_theme_key));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create and return a wrapped context with the default selected theme set.
|
||||
*
|
||||
* @param baseContext the base context for the wrapper
|
||||
* @return a wrapped-styled context
|
||||
*/
|
||||
public static Context getThemedContext(Context baseContext) {
|
||||
return new ContextThemeWrapper(baseContext, getThemeForService(baseContext, -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the selected theme without being styled to any service (see {@link #getThemeForService(Context, int)}).
|
||||
*
|
||||
* @param context context to get the selected theme
|
||||
* @return the selected style (the default one)
|
||||
*/
|
||||
@StyleRes
|
||||
public static int getDefaultTheme(Context context) {
|
||||
return getThemeForService(context, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public static int getDialogTheme(Context context) {
|
||||
return isLightThemeSelected(context) ? R.style.LightDialogTheme : R.style.DarkDialogTheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
@StyleRes
|
||||
public static int getThemeForService(Context context, int serviceId) {
|
||||
String lightTheme = context.getResources().getString(R.string.light_theme_key);
|
||||
String darkTheme = context.getResources().getString(R.string.dark_theme_key);
|
||||
String blackTheme = context.getResources().getString(R.string.black_theme_key);
|
||||
|
||||
String selectedTheme = getSelectedTheme(context);
|
||||
String selectedTheme = getSelectedThemeString(context);
|
||||
|
||||
int defaultTheme = R.style.DarkTheme;
|
||||
if (selectedTheme.equals(lightTheme)) defaultTheme = R.style.LightTheme;
|
||||
|
|
@ -83,19 +146,13 @@ public class ThemeHelper {
|
|||
return defaultTheme;
|
||||
}
|
||||
|
||||
public static String getSelectedTheme(Context context) {
|
||||
String themeKey = context.getString(R.string.theme_key);
|
||||
String defaultTheme = context.getResources().getString(R.string.default_theme_value);
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(themeKey, defaultTheme);
|
||||
}
|
||||
|
||||
@StyleRes
|
||||
public static int getSettingsThemeStyle(Context context) {
|
||||
String lightTheme = context.getResources().getString(R.string.light_theme_key);
|
||||
String darkTheme = context.getResources().getString(R.string.dark_theme_key);
|
||||
String blackTheme = context.getResources().getString(R.string.black_theme_key);
|
||||
|
||||
String selectedTheme = getSelectedTheme(context);
|
||||
String selectedTheme = getSelectedThemeString(context);
|
||||
|
||||
if (selectedTheme.equals(lightTheme)) return R.style.LightSettingsTheme;
|
||||
else if (selectedTheme.equals(blackTheme)) return R.style.BlackSettingsTheme;
|
||||
|
|
@ -113,4 +170,24 @@ public class ThemeHelper {
|
|||
a.recycle();
|
||||
return attributeResourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a color from an attr styled according to the the context's theme.
|
||||
*/
|
||||
public static int resolveColorFromAttr(Context context, @AttrRes int attrColor) {
|
||||
final TypedValue value = new TypedValue();
|
||||
context.getTheme().resolveAttribute(attrColor, value, true);
|
||||
|
||||
if (value.resourceId != 0) {
|
||||
return ContextCompat.getColor(context, value.resourceId);
|
||||
}
|
||||
|
||||
return value.data;
|
||||
}
|
||||
|
||||
private static String getSelectedThemeString(Context context) {
|
||||
String themeKey = context.getString(R.string.theme_key);
|
||||
String defaultTheme = context.getResources().getString(R.string.default_theme_value);
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(themeKey, defaultTheme);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue