Migrate to opoc/basefragment; Fix redundant casts

This commit is contained in:
Gregor Santner 2018-03-30 00:38:31 +02:00
parent a88dc5d13c
commit 08242760bb
No known key found for this signature in database
GPG Key ID: 7E83A7834AECB009
15 changed files with 176 additions and 109 deletions

View File

@ -26,6 +26,8 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
@ -157,10 +159,14 @@ public class AboutActivity extends ThemedActivity
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about__fragment_about, container, false);
ButterKnife.bind(this, rootView);
protected int getLayoutResId() {
return R.layout.about__fragment_about;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
if (isAdded()) {
try {
PackageInfo pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
@ -170,7 +176,6 @@ public class AboutActivity extends ThemedActivity
e.printStackTrace();
}
}
return rootView;
}
@Override
@ -244,10 +249,14 @@ public class AboutActivity extends ThemedActivity
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about__fragment_license, container, false);
ButterKnife.bind(this, rootView);
final Context context = rootView.getContext();
protected int getLayoutResId() {
return R.layout.about__fragment_license;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
accentColor = ContextUtils.get().colorToHexString(ThemeHelper.getAccentColor());
maintainers.setTextFormatted(getString(R.string.fragment_license__maintainers_text,
@ -256,7 +265,6 @@ public class AboutActivity extends ThemedActivity
ContextUtils.get().loadMarkdownForTextViewFromRaw(R.raw.contributors, "")));
thirdPartyLibs.setTextFormatted(
ContextUtils.get().loadMarkdownForTextViewFromRaw(R.raw.license_third_party, ""));
return rootView;
}
@OnClick({R.id.fragment_license__leafpic_button, R.id.fragment_license__license_button})

View File

@ -22,6 +22,7 @@ import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@ -68,13 +69,12 @@ public class AspectListFragment extends ThemedFragment implements OnSomethingCli
protected DiasporaUrlHelper urls;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.recycler_list__fragment, container, false);
protected int getLayoutResId() {
return R.layout.recycler_list__fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
app = (App) getActivity().getApplication();

View File

@ -835,7 +835,7 @@ public class MainActivity extends ThemedActivity
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
@SuppressLint("InflateParams") View layout = getLayoutInflater().inflate(R.layout.ui__dialog_search__people_tags, null, false);
final EditText input = (EditText) layout.findViewById(R.id.dialog_search__input);
final EditText input = layout.findViewById(R.id.dialog_search__input);
input.setMaxLines(1);
input.setSingleLine(true);
ThemeHelper.updateEditTextColor(input);

View File

@ -92,16 +92,14 @@ public class PodSelectionFragment extends ThemedFragment implements SearchView.O
private String filterString = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
View view = inflater.inflate(R.layout.podselection__fragment, container, false);
ButterKnife.bind(this, view);
return view;
protected int getLayoutResId() {
return R.layout.podselection__fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
app = (App) getActivity().getApplication();
appSettings = app.getSettings();
@ -204,7 +202,7 @@ public class PodSelectionFragment extends ThemedFragment implements SearchView.O
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
TextView textView = view.findViewById(android.R.id.text1);
textView.setTextColor(appSettings.isAmoledColorMode() ? Color.GRAY : Color.BLACK);
return view;
}

View File

@ -305,10 +305,10 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
final ThemedAlertDialogBuilder builder = new ThemedAlertDialogBuilder(context, appSettings);
builder.setView(dialogLayout);
final FrameLayout titleBackground = (FrameLayout) dialogLayout.findViewById(R.id.color_picker_dialog__title_background);
final TextView title = (TextView) dialogLayout.findViewById(R.id.color_picker_dialog__title);
final LineColorPicker base = (LineColorPicker) dialogLayout.findViewById(R.id.color_picker_dialog__base_picker);
final LineColorPicker shade = (LineColorPicker) dialogLayout.findViewById(R.id.color_picker_dialog__shade_picker);
final FrameLayout titleBackground = dialogLayout.findViewById(R.id.color_picker_dialog__title_background);
final TextView title = dialogLayout.findViewById(R.id.color_picker_dialog__title);
final LineColorPicker base = dialogLayout.findViewById(R.id.color_picker_dialog__base_picker);
final LineColorPicker shade = dialogLayout.findViewById(R.id.color_picker_dialog__shade_picker);
title.setText(type == 1 ? R.string.pref_title__primary_color : R.string.pref_title__accent_color);
title.setTextColor(getResources().getColor(R.color.white));

View File

@ -67,9 +67,8 @@ public class TagListFragment extends ThemedFragment implements OnSomethingClickL
protected DiasporaUrlHelper urls;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.recycler_list__fragment, container, false);
protected int getLayoutResId() {
return R.layout.recycler_list__fragment;
}
@Override

View File

@ -41,7 +41,7 @@ public class ThemedCheckBoxPreference extends CheckBoxPreference implements Them
@Override
public void setColors() {
CheckBox checkBox = (CheckBox) rootLayout.findViewById(android.R.id.checkbox);
CheckBox checkBox = rootLayout.findViewById(android.R.id.checkbox);
ThemeHelper.getInstance(AppSettings.get());
ThemeHelper.updateCheckBoxColor(checkBox);
}

View File

@ -38,7 +38,7 @@ public class ThemedColorPickerPreference extends Preference implements Themeable
@Override
protected void onBindView(View view) {
super.onBindView(view);
colorPreview = (ImageView) view.findViewById(android.R.id.icon);
colorPreview = view.findViewById(android.R.id.icon);
setColors();
}

View File

@ -21,14 +21,14 @@ package com.github.dfa.diaspora_android.ui.theme;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.util.AppSettings;
import net.gsantner.opoc.activity.CustomFragment;
import net.gsantner.opoc.activity.GsFragmentBase;
/**
* Fragment that supports color schemes
* Created by vanitas on 06.10.16.
*/
public abstract class ThemedFragment extends CustomFragment {
public abstract class ThemedFragment extends GsFragmentBase {
protected AppSettings getAppSettings() {
return ((App) getActivity().getApplication()).getSettings();
}

View File

@ -36,7 +36,7 @@ public class ThemedPreferenceCategory extends PreferenceCategory implements Them
@Override
protected View onCreateView(ViewGroup parent) {
View rootLayout = super.onCreateView(parent);
this.titleTextView = (TextView) rootLayout.findViewById(android.R.id.title);
this.titleTextView = rootLayout.findViewById(android.R.id.title);
setColors();
return rootLayout;
}

View File

@ -27,7 +27,7 @@ public class ThemedVisibilityPreference extends ThemedCheckBoxPreference {
@Override
public void setColors() {
CheckBox checkBox = (CheckBox) rootLayout.findViewById(android.R.id.checkbox);
CheckBox checkBox = rootLayout.findViewById(android.R.id.checkbox);
checkBox.setButtonDrawable(R.drawable.ic_visibility_selector);
ThemeHelper.getInstance(AppSettings.get());
ThemeHelper.updateCheckBoxColor(checkBox);

View File

@ -22,7 +22,7 @@ public class AndroidBug5497Workaround {
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {

View File

@ -64,7 +64,6 @@ import java.util.Locale;
public class BrowserFragment extends ThemedFragment {
public static final String TAG = "com.github.dfa.diaspora_android.BrowserFragment";
protected View rootLayout;
protected ContextMenuWebView webView;
protected ProgressBar progressBar;
protected AppSettings appSettings;
@ -74,18 +73,11 @@ public class BrowserFragment extends ThemedFragment {
protected String pendingUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
if (rootLayout == null) {
LayoutInflater inflater1 = inflater.cloneInContext(new MutableContextWrapper(getContext()));
rootLayout = inflater1.inflate(R.layout.browser__fragment, container, false);
} else {
MutableContextWrapper context = (MutableContextWrapper) rootLayout.getContext();
context.setBaseContext(getContext());
}
return rootLayout;
protected int getLayoutResId() {
return R.layout.browser__fragment;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
AppLog.d(this, "onViewCreated()");
@ -96,7 +88,7 @@ public class BrowserFragment extends ThemedFragment {
}
if (this.webView == null) {
this.webView = (ContextMenuWebView) view.findViewById(R.id.webView);
this.webView = view.findViewById(R.id.webView);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
@ -108,7 +100,7 @@ public class BrowserFragment extends ThemedFragment {
}
if (this.progressBar == null) {
this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
this.progressBar = view.findViewById(R.id.progressBar);
}
if (pendingUrl != null) {
@ -125,8 +117,8 @@ public class BrowserFragment extends ThemedFragment {
public void onDestroyView() {
super.onDestroyView();
if (getRetainInstance() && rootLayout.getParent() instanceof ViewGroup) {
((ViewGroup) rootLayout.getParent()).removeView(rootLayout);
if (getRetainInstance() && getView() != null && getView().getParent() instanceof ViewGroup) {
((ViewGroup) getView().getParent()).removeView(getView());
}
}

View File

@ -1,59 +0,0 @@
/*
This file is part of the dandelion*.
dandelion* 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.
dandelion* 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 the dandelion*.
If not, see <http://www.gnu.org/licenses/>.
*/
package net.gsantner.opoc.activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
/**
* Customized abstract Fragment class with some useful methods
* Created by vanitas on 21.09.16.
*/
public abstract class CustomFragment extends Fragment {
public static final String TAG = "net.gsantner.opoc.activity.CustomFragment";
/**
* We have an optionsMenu
*
* @param savedInstanceState state
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
/**
* Return the tag used to identify the Fragment.
*
* @return tag
*/
public abstract String getFragmentTag();
/**
* Return true if the fragment reacted to a back button press, false else.
* In case the fragment returned false, the parent activity should handle the backPress.
*
* @return did we react to the back press?
*/
public abstract boolean onBackPressed();
}

View File

@ -0,0 +1,129 @@
/*#######################################################
*
* Maintained by Gregor Santner, 2017-
* https://gsantner.net/
*
* License: Apache 2.0
* https://github.com/gsantner/opoc/#licensing
* https://www.apache.org/licenses/LICENSE-2.0
*
#########################################################*/
package net.gsantner.opoc.activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import net.gsantner.opoc.util.ContextUtils;
import butterknife.ButterKnife;
/**
* A common base fragment to extend from
*/
public abstract class GsFragmentBase extends Fragment {
private boolean _fragmentFirstTimeVisible = true;
private final Object _fragmentFirstTimeVisibleSync = new Object();
protected ContextUtils _cu;
protected Bundle _savedInstanceState = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
/**
* Inflate the fragments layout. Don't override this method, just supply the needed
* {@link LayoutRes} via abstract method {@link #getLayoutResId()}, super does the rest
*/
@Deprecated
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
_cu = new ContextUtils(inflater.getContext());
_cu.setAppLanguage(getAppLanguage());
_savedInstanceState = savedInstanceState;
View view = inflater.inflate(getLayoutResId(), container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.postDelayed(() -> {
synchronized (_fragmentFirstTimeVisibleSync) {
if (getUserVisibleHint() && isVisible() && _fragmentFirstTimeVisible) {
_fragmentFirstTimeVisible = false;
onFragmentFirstTimeVisible();
}
}
}, 1);
}
/**
* Get a tag from the fragment, allows faster distinction
*
* @return This fragments tag
*/
public abstract String getFragmentTag();
/**
* Get the layout to be inflated in the fragment
*
* @return Layout resource id
*/
@LayoutRes
protected abstract int getLayoutResId();
/**
* Event to be called when the back button was pressed
* True should be returned when this was handled by the fragment
* and no further handling in the view hierarchy is needed
*
* @return True if back handled by fragment
*/
public boolean onBackPressed() {
return false;
}
/**
* Set the language to be used in this fragment
* Defaults to resolve the language from sharedpreferences: pref_key__language
*
* @return Empty string for system language, or an android locale code
*/
public String getAppLanguage() {
if (getContext() != null) {
return getContext().getSharedPreferences("app", Context.MODE_PRIVATE)
.getString("pref_key__language", "");
}
return "";
}
/**
* This will be called when this fragment gets the first time visible
*/
public void onFragmentFirstTimeVisible() {
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
synchronized (_fragmentFirstTimeVisibleSync) {
if (isVisibleToUser && _fragmentFirstTimeVisible) {
_fragmentFirstTimeVisible = false;
onFragmentFirstTimeVisible();
}
}
}
}