mirror of
https://github.com/FatttSnake/OxygenToolbox.git
synced 2026-04-06 09:41:26 +08:00
Optimized NavigationBar.
Added BottomNavigationView. Removed FloatingActionButton.
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package com.fatapp.oxygentoolbox;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.Menu;
|
||||
|
||||
import com.fatapp.oxygentoolbox.util.ResourceUtil;
|
||||
import com.fatapp.oxygentoolbox.util.VibratorController;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.navigation.fragment.NavHostFragment;
|
||||
@@ -26,14 +25,15 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private AppBarConfiguration mAppBarConfiguration;
|
||||
|
||||
private Toolbar toolbar;
|
||||
private FloatingActionButton fab;
|
||||
private DrawerLayout drawer;
|
||||
private CoordinatorLayout mainPage;
|
||||
private Toolbar toolbar;
|
||||
private NavigationView navigationView;
|
||||
|
||||
private void initView() {
|
||||
toolbar = findViewById(R.id.toolbar);
|
||||
fab = findViewById(R.id.fab);
|
||||
drawer = findViewById(R.id.drawer_layout);
|
||||
mainPage = findViewById(R.id.main_page);
|
||||
toolbar = findViewById(R.id.toolbar);
|
||||
navigationView = findViewById(R.id.nav_view);
|
||||
mainActivity = this;
|
||||
}
|
||||
@@ -53,21 +53,15 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
mainPage.setPadding(0, ResourceUtil.getStatusBarHeight(getWindow(), getApplicationContext()), 0, 0);
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
fab.setOnClickListener(view -> {
|
||||
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();*/
|
||||
});
|
||||
navigationView.inflateHeaderView(R.layout.nav_header_main);
|
||||
navigationView.inflateMenu(R.menu.activity_main_drawer);
|
||||
navigationView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
|
||||
if (navigationView.getMenu().getItem(0).isChecked()) {
|
||||
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
|
||||
fab.setVisibility(View.GONE);
|
||||
}
|
||||
drawer.setDrawerLockMode(navigationView.getMenu().getItem(0).isChecked()
|
||||
? DrawerLayout.LOCK_MODE_UNLOCKED
|
||||
: DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
|
||||
});
|
||||
navigationView.getMenu().getItem(4).setOnMenuItemClickListener(item -> {
|
||||
finish();
|
||||
|
||||
@@ -17,8 +17,17 @@ public class SplashActivity extends AppCompatActivity {
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_splash);
|
||||
|
||||
startActivity(new Intent(getApplicationContext(), MainActivity.class));
|
||||
finish();
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
sleep(0);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
startActivity(new Intent(getApplicationContext(), MainActivity.class));
|
||||
finish();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,7 @@ public class AboutFragment extends Fragment {
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
aboutViewModel =
|
||||
new ViewModelProvider(this).get(AboutViewModel.class);
|
||||
aboutViewModel = new ViewModelProvider(this).get(AboutViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_about, container, false);
|
||||
final TextView textView = root.findViewById(R.id.text_about);
|
||||
aboutViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
|
||||
@@ -1,30 +1,20 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||
import androidx.viewpager2.widget.ViewPager2;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
import com.fatapp.oxygentoolbox.layout.FoldLayout;
|
||||
import com.fatapp.oxygentoolbox.util.ToolsLauncher;
|
||||
import com.fatapp.oxygentoolbox.util.ToolsList;
|
||||
import com.fatapp.oxygentoolbox.util.VibratorController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.fatapp.oxygentoolbox.ui.home.fav.FavFragment;
|
||||
import com.fatapp.oxygentoolbox.ui.home.tools.ToolsFragment;
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
public class HomeFragment extends Fragment {
|
||||
|
||||
@@ -32,109 +22,43 @@ public class HomeFragment extends Fragment {
|
||||
|
||||
private HomeViewModel homeViewModel;
|
||||
|
||||
private LinearLayout foldLayoutsLinearLayout;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
homeViewModel =
|
||||
new ViewModelProvider(this).get(HomeViewModel.class);
|
||||
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
|
||||
root = inflater.inflate(R.layout.fragment_home, container, false);
|
||||
|
||||
//init
|
||||
initView();
|
||||
initLayout();
|
||||
ViewPager2 bottomNavViewPager = root.findViewById(R.id.bottom_nav_view_pager);
|
||||
BottomNavigationView bottomNavigationView = root.findViewById(R.id.bottom_navigation_view);
|
||||
|
||||
bottomNavViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback(){
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
bottomNavigationView.getMenu().getItem(position).setChecked(true);
|
||||
}
|
||||
});
|
||||
bottomNavigationView.setOnItemSelectedListener(item -> {
|
||||
bottomNavViewPager.setCurrentItem(item.getOrder());
|
||||
return true;
|
||||
});
|
||||
bottomNavViewPager.setAdapter(new FragmentStateAdapter(this) {
|
||||
@NonNull
|
||||
@Override
|
||||
public Fragment createFragment(int position) {
|
||||
switch (position) {
|
||||
case 0:
|
||||
return new ToolsFragment();
|
||||
case 1:
|
||||
return new FavFragment();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 2;
|
||||
}
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
foldLayoutsLinearLayout = root.findViewById(R.id.fold_layouts_linear_layout);
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
initFoldLayout();
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void initFoldLayout() {
|
||||
try {
|
||||
ToolsList.init(getResources().getAssets().open("json/BasicTools.json"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(getContext(), R.string.init_tools_failed, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
for (ToolsList.Tool tool : ToolsList.getToolList()) {
|
||||
View foldLayoutBodyLayout = getLayoutInflater().inflate(R.layout.fold_layout_body, null);
|
||||
ViewGroup autoLinefeedLayout = foldLayoutBodyLayout.findViewById(R.id.auto_linefeed_layout);
|
||||
|
||||
for (ToolsList.Button button : tool.getButtonList()) {
|
||||
View toolButtonLayout = getLayoutInflater().inflate(R.layout.tool_button, null);
|
||||
Button toolButton = toolButtonLayout.findViewById(R.id.tool_button);
|
||||
toolButton.setText(button.getText());
|
||||
|
||||
toolButton.setOnClickListener(v -> ToolsLauncher.launch(getContext(), button.getActivity()));
|
||||
|
||||
toolButton.setOnTouchListener((v, event) -> {
|
||||
if (event.getAction()== MotionEvent.ACTION_DOWN) {
|
||||
v.animate().translationZ(8f).setDuration(100L);
|
||||
}
|
||||
if (event.getAction() == MotionEvent.ACTION_UP) {
|
||||
v.animate().translationZ(0).setDuration(100L);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
toolButton.setOnLongClickListener(v -> {
|
||||
v.animate().translationZ(0).setDuration(100L);
|
||||
VibratorController.vibrate(1);
|
||||
return false;
|
||||
});
|
||||
|
||||
autoLinefeedLayout.addView(toolButtonLayout);
|
||||
}
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
viewList.add(foldLayoutBodyLayout);
|
||||
|
||||
View foldLayoutHead = getLayoutInflater().inflate(R.layout.fold_layout, null);
|
||||
FoldLayout foldLayout = foldLayoutHead.findViewById(R.id.fold_layout);
|
||||
((TextView) foldLayout.findViewById(R.id.fold_layout_text_view)).setText(tool.getFoldLayoutTitle());
|
||||
|
||||
TextView foldLayoutIcon = foldLayout.findViewById(R.id.fold_layout_icon);
|
||||
foldLayoutIcon.setTypeface(Typeface.createFromAsset(requireContext().getAssets(), tool.getFont()));
|
||||
foldLayoutIcon.setText(tool.getIcon());
|
||||
|
||||
foldLayout.addItemView(viewList);
|
||||
|
||||
foldLayoutsLinearLayout.removeAllViews();
|
||||
foldLayoutsLinearLayout.addView(foldLayoutHead);
|
||||
}
|
||||
|
||||
/*
|
||||
for (int i = 0; i < 10; i++) {
|
||||
View toolButton = getLayoutInflater().inflate(R.layout.tool_button, null);
|
||||
((Button) toolButton.findViewById(R.id.tool_button)).setText("Button");
|
||||
toolButton.findViewById(R.id.tool_button).setOnClickListener(v -> {
|
||||
BasicToolsLauncher.launch(0, getContext());
|
||||
});
|
||||
|
||||
View foldLayoutBody = getLayoutInflater().inflate(R.layout.fold_layout_body, null);
|
||||
ViewGroup layout_item_AutoLinefeedLayout = foldLayoutBody.findViewById(R.id.auto_linefeed_layout);
|
||||
layout_item_AutoLinefeedLayout.addView(toolButton);
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
viewList.add(foldLayoutBody);
|
||||
|
||||
View foldLayoutHead = getLayoutInflater().inflate(R.layout.fold_layout, null);
|
||||
FoldLayout foldLayout = foldLayoutHead.findViewById(R.id.fold_layout);
|
||||
((TextView) foldLayout.findViewById(R.id.fold_layout_text_view)).setText("FoldLayout_" + i);
|
||||
foldLayout.addItemView(viewList);
|
||||
|
||||
foldLayoutsLinearLayout.addView(foldLayoutHead);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home.fav;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
|
||||
public class FavFragment extends Fragment {
|
||||
|
||||
private View root;
|
||||
|
||||
private FavViewModel favViewModel;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
favViewModel = new ViewModelProvider(this).get(FavViewModel.class);
|
||||
root = inflater.inflate(R.layout.fragment_home_fav, container, false);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home.fav;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class FavViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public FavViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is home fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home.tools;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
import com.fatapp.oxygentoolbox.layout.FoldLayout;
|
||||
import com.fatapp.oxygentoolbox.util.ToolsLauncher;
|
||||
import com.fatapp.oxygentoolbox.util.ToolsList;
|
||||
import com.fatapp.oxygentoolbox.util.VibratorController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ToolsFragment extends Fragment {
|
||||
|
||||
private View root;
|
||||
|
||||
private ToolsViewModel toolsViewModel;
|
||||
|
||||
private LinearLayout foldLayoutsLinearLayout;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
toolsViewModel = new ViewModelProvider(this).get(ToolsViewModel.class);
|
||||
root = inflater.inflate(R.layout.fragment_home_tools, container, false);
|
||||
|
||||
//init
|
||||
initView();
|
||||
initLayout();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
foldLayoutsLinearLayout = root.findViewById(R.id.fold_layouts_linear_layout);
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
initFoldLayout();
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void initFoldLayout() {
|
||||
try {
|
||||
ToolsList.init(getResources().getAssets().open("json/BasicTools.json"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(getContext(), R.string.init_tools_failed, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
for (ToolsList.Tool tool : ToolsList.getToolList()) {
|
||||
View foldLayoutBodyLayout = getLayoutInflater().inflate(R.layout.fold_layout_body, null);
|
||||
ViewGroup autoLinefeedLayout = foldLayoutBodyLayout.findViewById(R.id.auto_linefeed_layout);
|
||||
|
||||
for (ToolsList.Button button : tool.getButtonList()) {
|
||||
View toolButtonLayout = getLayoutInflater().inflate(R.layout.tool_button, null);
|
||||
Button toolButton = toolButtonLayout.findViewById(R.id.tool_button);
|
||||
toolButton.setText(button.getText());
|
||||
|
||||
toolButton.setOnClickListener(v -> ToolsLauncher.launch(getContext(), button.getActivity()));
|
||||
|
||||
toolButton.setOnTouchListener((v, event) -> {
|
||||
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
||||
v.animate().translationZ(8f).setDuration(100L);
|
||||
}
|
||||
if (event.getAction() == MotionEvent.ACTION_UP) {
|
||||
v.animate().translationZ(0).setDuration(100L);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
toolButton.setOnLongClickListener(v -> {
|
||||
v.animate().translationZ(0).setDuration(100L);
|
||||
VibratorController.vibrate(1);
|
||||
return false;
|
||||
});
|
||||
|
||||
autoLinefeedLayout.addView(toolButtonLayout);
|
||||
}
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
viewList.add(foldLayoutBodyLayout);
|
||||
|
||||
View foldLayoutHead = getLayoutInflater().inflate(R.layout.fold_layout, null);
|
||||
FoldLayout foldLayout = foldLayoutHead.findViewById(R.id.fold_layout);
|
||||
((TextView) foldLayout.findViewById(R.id.fold_layout_text_view)).setText(tool.getFoldLayoutTitle());
|
||||
|
||||
TextView foldLayoutIcon = foldLayout.findViewById(R.id.fold_layout_icon);
|
||||
foldLayoutIcon.setTypeface(Typeface.createFromAsset(requireContext().getAssets(), tool.getFont()));
|
||||
foldLayoutIcon.setText(tool.getIcon());
|
||||
|
||||
foldLayout.addItemView(viewList);
|
||||
|
||||
foldLayoutsLinearLayout.removeAllViews();
|
||||
foldLayoutsLinearLayout.addView(foldLayoutHead);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
View toolButton = getLayoutInflater().inflate(R.layout.tool_button, null);
|
||||
((Button) toolButton.findViewById(R.id.tool_button)).setText("Button");
|
||||
toolButton.findViewById(R.id.tool_button).setOnClickListener(v -> {
|
||||
});
|
||||
|
||||
View foldLayoutBody = getLayoutInflater().inflate(R.layout.fold_layout_body, null);
|
||||
ViewGroup layout_item_AutoLinefeedLayout = foldLayoutBody.findViewById(R.id.auto_linefeed_layout);
|
||||
layout_item_AutoLinefeedLayout.addView(toolButton);
|
||||
|
||||
List<View> viewList = new ArrayList<>();
|
||||
viewList.add(foldLayoutBody);
|
||||
|
||||
View foldLayoutHead = getLayoutInflater().inflate(R.layout.fold_layout, null);
|
||||
FoldLayout foldLayout = foldLayoutHead.findViewById(R.id.fold_layout);
|
||||
((TextView) foldLayout.findViewById(R.id.fold_layout_text_view)).setText("FoldLayout_" + i);
|
||||
foldLayout.addItemView(viewList);
|
||||
|
||||
foldLayoutsLinearLayout.addView(foldLayoutHead);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.fatapp.oxygentoolbox.ui.home.tools;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
public class ToolsViewModel extends ViewModel {
|
||||
|
||||
private MutableLiveData<String> mText;
|
||||
|
||||
public ToolsViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is home fragment");
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,9 @@ import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
@@ -20,8 +17,7 @@ public class SettingFragment extends Fragment {
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
settingViewModel =
|
||||
new ViewModelProvider(this).get(SettingViewModel.class);
|
||||
settingViewModel = new ViewModelProvider(this).get(SettingViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_setting, container, false);
|
||||
/*
|
||||
final TextView textView = root.findViewById(R.id.text_setting);
|
||||
|
||||
@@ -18,10 +18,8 @@ public class ThemeFragment extends Fragment {
|
||||
|
||||
private ThemeViewModel themeViewModel;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
themeViewModel =
|
||||
new ViewModelProvider(this).get(ThemeViewModel.class);
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
themeViewModel = new ViewModelProvider(this).get(ThemeViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_theme, container, false);
|
||||
final TextView textView = root.findViewById(R.id.text_theme);
|
||||
themeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
package com.fatapp.oxygentoolbox.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Rect;
|
||||
import android.view.Window;
|
||||
|
||||
import androidx.annotation.AttrRes;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StyleRes;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
|
||||
import com.google.android.material.color.MaterialColors;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ResourceUtil {
|
||||
|
||||
@@ -28,4 +43,43 @@ public final class ResourceUtil {
|
||||
public static int getColor(int resId) {
|
||||
return sRes.getColor(resId);
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
public static int getThemeAttrColor(@NonNull Context context, @StyleRes int themeResId, @AttrRes int attrResId) {
|
||||
return MaterialColors.getColor(new ContextThemeWrapper(context, themeResId), attrResId, Color.WHITE);
|
||||
}
|
||||
|
||||
public static int getStatusBarHeight(Window window, Context context) {
|
||||
Rect localRect = new Rect();
|
||||
window.getDecorView().getWindowVisibleDisplayFrame(localRect);
|
||||
int mStatusBarHeight = localRect.top;
|
||||
if (mStatusBarHeight == 0) {
|
||||
try {
|
||||
@SuppressLint("PrivateApi")
|
||||
Class<?> localClass = Class.forName("com.android.internal.R$dimen");
|
||||
Object localObject = localClass.newInstance();
|
||||
int status_bar_height = Integer.parseInt(Objects.requireNonNull(localClass.getField("status_bar_height").get(localObject)).toString());
|
||||
mStatusBarHeight = context.getResources().getDimensionPixelSize(status_bar_height);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (mStatusBarHeight == 0) {
|
||||
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
mStatusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
}
|
||||
return mStatusBarHeight;
|
||||
}
|
||||
|
||||
public static int getNavigationBarHeight(Context context) {
|
||||
int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
|
||||
if (rid != 0) {
|
||||
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
||||
return context.getResources().getDimensionPixelSize(resourceId);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user