Use a non-reflect way to change how PreferenceActivity looks
Android P doesn't support invoking hidden API.
This commit is contained in:
1 parent
f922d41775
commit
8c9218e8af
3 files changed
+111
-86
No files matched your search
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2018 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.TextView;
|
||||
import com.hippo.ehviewer.R;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class PrettyPreferenceActivity extends AppCompatPreferenceActivity {
|
||||
|
||||
@Override
|
||||
public void setListAdapter(ListAdapter adapter) {
|
||||
int count = adapter.getCount();
|
||||
List<PreferenceActivity.Header> headers = new ArrayList<>(count);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
headers.add((PreferenceActivity.Header) adapter.getItem(i));
|
||||
}
|
||||
|
||||
super.setListAdapter(new HeaderAdapter(this, headers, R.layout.item_preference_header, true));
|
||||
}
|
||||
|
||||
private static class HeaderAdapter extends ArrayAdapter<PreferenceActivity.Header> {
|
||||
private static class HeaderViewHolder {
|
||||
ImageView icon;
|
||||
TextView title;
|
||||
TextView summary;
|
||||
}
|
||||
|
||||
private LayoutInflater mInflater;
|
||||
private int mLayoutResId;
|
||||
private boolean mRemoveIconIfEmpty;
|
||||
|
||||
private HeaderAdapter(Context context, List<PreferenceActivity.Header> objects, int layoutResId,
|
||||
boolean removeIconBehavior) {
|
||||
super(context, 0, objects);
|
||||
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mLayoutResId = layoutResId;
|
||||
mRemoveIconIfEmpty = removeIconBehavior;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
|
||||
HeaderViewHolder holder;
|
||||
View view;
|
||||
|
||||
if (convertView == null) {
|
||||
view = mInflater.inflate(mLayoutResId, parent, false);
|
||||
holder = new HeaderViewHolder();
|
||||
holder.icon = view.findViewById(android.R.id.icon);
|
||||
holder.title = view.findViewById(android.R.id.title);
|
||||
holder.summary = view.findViewById(android.R.id.summary);
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
view = convertView;
|
||||
holder = (HeaderViewHolder) view.getTag();
|
||||
}
|
||||
|
||||
// All view fields must be updated every time, because the view may be recycled
|
||||
PreferenceActivity.Header header = getItem(position);
|
||||
if (mRemoveIconIfEmpty) {
|
||||
if (header.iconRes == 0) {
|
||||
holder.icon.setVisibility(View.GONE);
|
||||
} else {
|
||||
holder.icon.setVisibility(View.VISIBLE);
|
||||
holder.icon.setImageResource(header.iconRes);
|
||||
}
|
||||
} else {
|
||||
holder.icon.setImageResource(header.iconRes);
|
||||
}
|
||||
holder.title.setText(header.getTitle(getContext().getResources()));
|
||||
CharSequence summary = header.getSummary(getContext().getResources());
|
||||
if (!TextUtils.isEmpty(summary)) {
|
||||
holder.summary.setVisibility(View.VISIBLE);
|
||||
holder.summary.setText(summary);
|
||||
} else {
|
||||
holder.summary.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,13 +22,13 @@ import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.WindowManager;
|
||||
import com.google.analytics.tracking.android.EasyTracker;
|
||||
import com.hippo.app.AppCompatPreferenceActivity;
|
||||
import com.hippo.app.PrettyPreferenceActivity;
|
||||
import com.hippo.content.ContextLocalWrapper;
|
||||
import com.hippo.ehviewer.EhApplication;
|
||||
import com.hippo.ehviewer.Settings;
|
||||
import java.util.Locale;
|
||||
|
||||
public abstract class EhPreferenceActivity extends AppCompatPreferenceActivity {
|
||||
public abstract class EhPreferenceActivity extends PrettyPreferenceActivity {
|
||||
|
||||
private boolean mTrackStarted;
|
||||
|
||||
|
||||
@@ -17,31 +17,21 @@
|
||||
package com.hippo.ehviewer.ui;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.ActionBarDrawerToggle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListAdapter;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.ui.fragment.AboutFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.AdvancedFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.DownloadFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.EhFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.ReadFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.PrivacyFragment;
|
||||
import com.hippo.ehviewer.ui.fragment.ReadFragment;
|
||||
import com.hippo.util.DrawableManager;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
public final class SettingsActivity extends EhPreferenceActivity {
|
||||
@@ -57,77 +47,6 @@ public final class SettingsActivity extends EhPreferenceActivity {
|
||||
PrivacyFragment.class.getName(),
|
||||
};
|
||||
|
||||
private class FakeLayoutInflater extends LayoutInflater {
|
||||
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
protected FakeLayoutInflater(LayoutInflater inflater) {
|
||||
super(null);
|
||||
mInflater = inflater;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LayoutInflater cloneInContext(Context newContext) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
|
||||
return mInflater.inflate(R.layout.item_preference_header, root, attachToRoot);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("TryWithIdenticalCatches")
|
||||
private void replaceHeaderLayoutResId() {
|
||||
try {
|
||||
ListAdapter adapter = getListAdapter();
|
||||
Class headerAdapterClazz = Class.forName("android.preference.PreferenceActivity$HeaderAdapter");
|
||||
if (!headerAdapterClazz.isInstance(adapter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean ok = false;
|
||||
|
||||
// For lollipop and above this work
|
||||
try {
|
||||
Field field = headerAdapterClazz.getDeclaredField("mLayoutResId");
|
||||
field.setAccessible(true);
|
||||
field.setInt(adapter, R.layout.item_preference_header);
|
||||
|
||||
ok = true;
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// For pre-lollipop this work
|
||||
if (!ok) {
|
||||
try {
|
||||
Field field = headerAdapterClazz.getDeclaredField("mInflater");
|
||||
field.setAccessible(true);
|
||||
field.set(adapter, new FakeLayoutInflater((LayoutInflater) field.get(adapter)));
|
||||
|
||||
ok = true;
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassCastException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
getListView().setDivider(new ColorDrawable(Color.TRANSPARENT));
|
||||
getListView().setDividerHeight(0);
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void setActionBarUpIndicator(Drawable drawable) {
|
||||
ActionBarDrawerToggle.Delegate delegate = getDrawerToggleDelegate();
|
||||
if (delegate != null) {
|
||||
@@ -145,8 +64,6 @@ public final class SettingsActivity extends EhPreferenceActivity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setActionBarUpIndicator(DrawableManager.getDrawable(this, R.drawable.v_arrow_left_dark_x24));
|
||||
|
||||
replaceHeaderLayoutResId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user