used compat switch

This commit is contained in:
Mariotaku Lee 2015-04-22 23:30:45 +08:00
parent 2d4be1f7e7
commit fb823d7003
12 changed files with 86 additions and 296 deletions

View File

@ -228,6 +228,7 @@ public class SettingsActivity extends BasePreferenceActivity {
final ListView listView = getListView();
if (listView != null) {
listView.setDivider(new EmptyDrawable());
listView.setChoiceMode(isMultiPane() ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
listView.setDividerHeight(0);
final LayoutParams lp = listView.getLayoutParams();
if (lp instanceof MarginLayoutParams) {

View File

@ -33,7 +33,6 @@ import android.view.MenuInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.R;
@ -41,73 +40,73 @@ import org.mariotaku.twidere.model.ParcelableAccount;
import org.mariotaku.twidere.util.UserColorNameUtils;
public abstract class BaseAccountPreferenceFragment extends PreferenceFragment implements Constants,
OnCheckedChangeListener, OnSharedPreferenceChangeListener {
OnCheckedChangeListener, OnSharedPreferenceChangeListener {
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
final PreferenceManager pm = getPreferenceManager();
final ParcelableAccount account = getArguments().getParcelable(EXTRA_ACCOUNT);
final String preferenceName = ACCOUNT_PREFERENCES_NAME_PREFIX
+ (account != null ? account.account_id : "unknown");
pm.setSharedPreferencesName(preferenceName);
addPreferencesFromResource(getPreferencesResource());
final SharedPreferences prefs = pm.getSharedPreferences();
prefs.registerOnSharedPreferenceChangeListener(this);
final Activity activity = getActivity();
final Intent intent = activity.getIntent();
if (account != null && intent.hasExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT)) {
final String name = UserColorNameUtils.getDisplayName(getActivity(), account.account_id, account.name, account.screen_name);
activity.setTitle(name);
}
updatePreferenceScreen();
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
final PreferenceManager pm = getPreferenceManager();
final ParcelableAccount account = getArguments().getParcelable(EXTRA_ACCOUNT);
final String preferenceName = ACCOUNT_PREFERENCES_NAME_PREFIX
+ (account != null ? account.account_id : "unknown");
pm.setSharedPreferencesName(preferenceName);
addPreferencesFromResource(getPreferencesResource());
final SharedPreferences prefs = pm.getSharedPreferences();
prefs.registerOnSharedPreferenceChangeListener(this);
final Activity activity = getActivity();
final Intent intent = activity.getIntent();
if (account != null && intent.hasExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT)) {
final String name = UserColorNameUtils.getDisplayName(getActivity(), account.account_id, account.name, account.screen_name);
activity.setTitle(name);
}
updatePreferenceScreen();
}
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
final SharedPreferences.Editor editor = prefs.edit();
if (prefs.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()) != isChecked) {
editor.putBoolean(getSwitchPreferenceKey(), isChecked);
editor.apply();
}
}
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
final SharedPreferences.Editor editor = prefs.edit();
if (prefs.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()) != isChecked) {
editor.putBoolean(getSwitchPreferenceKey(), isChecked);
editor.apply();
}
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.menu_switch_preference, menu);
final View actionView = menu.findItem(MENU_TOGGLE).getActionView();
final Switch toggle = (Switch) actionView.findViewById(android.R.id.toggle);
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
toggle.setOnCheckedChangeListener(this);
toggle.setChecked(prefs.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()));
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.menu_switch_preference, menu);
final View actionView = menu.findItem(MENU_TOGGLE).getActionView();
final CompoundButton toggle = (CompoundButton) actionView.findViewById(android.R.id.toggle);
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
toggle.setOnCheckedChangeListener(this);
toggle.setChecked(prefs.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()));
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if (key.equals(getSwitchPreferenceKey())) {
updatePreferenceScreen();
}
}
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if (key.equals(getSwitchPreferenceKey())) {
updatePreferenceScreen();
}
}
protected ParcelableAccount getAccount() {
final Bundle args = getArguments();
if (args == null) return null;
return args.getParcelable(EXTRA_ACCOUNT);
}
protected ParcelableAccount getAccount() {
final Bundle args = getArguments();
if (args == null) return null;
return args.getParcelable(EXTRA_ACCOUNT);
}
protected abstract int getPreferencesResource();
protected abstract int getPreferencesResource();
protected abstract boolean getSwitchPreferenceDefault();
protected abstract boolean getSwitchPreferenceDefault();
protected abstract String getSwitchPreferenceKey();
protected abstract String getSwitchPreferenceKey();
private void updatePreferenceScreen() {
final PreferenceScreen screen = getPreferenceScreen();
final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
if (screen == null || sharedPreferences == null) return;
screen.setEnabled(sharedPreferences.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()));
}
private void updatePreferenceScreen() {
final PreferenceScreen screen = getPreferenceScreen();
final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
if (screen == null || sharedPreferences == null) return;
screen.setEnabled(sharedPreferences.getBoolean(getSwitchPreferenceKey(), getSwitchPreferenceDefault()));
}
}

View File

@ -105,7 +105,7 @@ public abstract class AccountsListPreference extends PreferenceCategory implemen
private final String mSwitchKey;
private final boolean mSwitchDefault;
private Switch mToggle;
private CompoundButton mToggle;
public AccountItemPreference(final Context context, final ParcelableAccount account, final String switchKey,
final boolean switchDefault) {
@ -169,7 +169,7 @@ public abstract class AccountsListPreference extends PreferenceCategory implemen
protected View onCreateView(ViewGroup parent) {
final View view = super.onCreateView(parent);
view.findViewById(R.id.settings).setOnClickListener(this);
final Switch toggle = (Switch) view.findViewById(android.R.id.toggle);
final CompoundButton toggle = (CompoundButton) view.findViewById(android.R.id.toggle);
toggle.setOnCheckedChangeListener(this);
mToggle = toggle;
return view;
@ -191,7 +191,7 @@ public abstract class AccountsListPreference extends PreferenceCategory implemen
if (summaryView instanceof TextView) {
((TextView) summaryView).setSingleLine(true);
}
final Switch toggle = (Switch) view.findViewById(android.R.id.toggle);
final CompoundButton toggle = (CompoundButton) view.findViewById(android.R.id.toggle);
if (mSwitchKey != null) {
toggle.setChecked(mSwitchPreference.getBoolean(mSwitchKey, mSwitchDefault));
}

View File

@ -1,84 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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.mariotaku.twidere.preference;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.preference.SwitchPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.fragment.SettingsDetailsFragment;
import org.mariotaku.twidere.util.ViewUtils;
/**
* Created by mariotaku on 15/4/7.
*/
public class SwitchSettingsDetailsPreference extends SwitchPreference implements Constants {
private View mView;
public SwitchSettingsDetailsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.src});
setFragment(SettingsDetailsFragment.class.getName());
final Bundle extras = getExtras();
extras.putInt(EXTRA_RESID, a.getResourceId(0, 0));
a.recycle();
}
public SwitchSettingsDetailsPreference(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.switchPreferenceStyle);
}
public SwitchSettingsDetailsPreference(Context context) {
this(context, null);
}
@Override
protected void onBindView(@NonNull View view) {
super.onBindView(view);
if (view instanceof ViewGroup) {
((ViewGroup) view).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
final Switch switchView = ViewUtils.findViewByType(view, Switch.class);
if (switchView != null) {
switchView.setClickable(true);
switchView.setFocusable(true);
}
}
@Override
protected View onCreateView(ViewGroup parent) {
if (mView != null) return mView;
return mView = super.onCreateView(parent);
}
@Override
protected void onClick() {
}
}

View File

@ -70,11 +70,12 @@ public class OAuthPasswordAuthenticator implements Constants {
if (e.isCausedByNetworkIssue()) throw new AuthenticationException(e);
throw new AuthenticityTokenException();
}
HttpResponse authorizePage = null, authorizeResult = null;
try {
final String oauthToken = requestToken.getToken();
final String authorizationUrl = requestToken.getAuthorizationURL();
final HashMap<String, String> inputMap = new HashMap<>();
final HttpResponse authorizePage = client.get(authorizationUrl, authorizationUrl, null, null, null);
authorizePage = client.get(authorizationUrl, authorizationUrl, null, null, null);
final List<String> cookieHeaders = authorizePage.getResponseHeaders("Set-Cookie");
readInputFromHtml(authorizePage.asReader(),
inputMap, INPUT_AUTHENTICITY_TOKEN, INPUT_REDIRECT_AFTER_LOGIN);
@ -104,12 +105,26 @@ public class OAuthPasswordAuthenticator implements Constants {
}
}
requestHeaders.put("Cookie", modifiedCookieHeaders);
final String oauthPin = readOAuthPINFromHtml(client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl,
params.toArray(new HttpParameter[params.size()]), requestHeaders).asReader());
authorizeResult = client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl,
params.toArray(new HttpParameter[params.size()]), requestHeaders);
final String oauthPin = readOAuthPINFromHtml(authorizeResult.asReader());
if (isEmpty(oauthPin)) throw new WrongUserPassException();
return twitter.getOAuthAccessToken(requestToken, oauthPin);
} catch (final IOException | TwitterException | NullPointerException | XmlPullParserException e) {
throw new AuthenticationException(e);
} finally {
if (authorizePage != null) {
try {
authorizePage.disconnect();
} catch (IOException ignore) {
}
}
if (authorizeResult != null) {
try {
authorizeResult.disconnect();
} catch (IOException ignore) {
}
}
}
}

View File

@ -1,43 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Twidere - Twitter client for Android
~
~ Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
~
~ 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/>.
-->
<!-- Layout of a header item in PreferenceActivity. -->
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="?android:colorBackground"
app:cardCornerRadius="0dp"
app:cardElevation="@dimen/elevation_settings_item">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="@dimen/element_spacing_normal"
android:singleLine="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
</android.support.v7.widget.CardView>

View File

@ -1,75 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2012 The Android Open Source Project
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.
-->
<!-- Layout of a header item in PreferenceActivity. -->
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="?android:colorBackground"
app:cardCornerRadius="0dp"
app:cardElevation="@dimen/elevation_settings_item">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal">
<ImageView
android:id="@android:id/icon"
android:layout_width="@dimen/header_icon_width"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/element_spacing_large"
android:layout_marginRight="@dimen/element_spacing_large"
android:layout_weight="0"/>
<LinearLayout
android:id="@android:id/content"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="?android:dividerVertical"
android:gravity="center_vertical"
android:orientation="vertical"
android:showDividers="end">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Twidere - Twitter client for Android
~
~ Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
~
~ 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/>.
-->
<Space
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/element_spacing_normal"/>

View File

@ -25,7 +25,7 @@
android:orientation="horizontal"
android:padding="@dimen/element_spacing_normal">
<Switch
<android.support.v7.widget.SwitchCompat
android:id="@android:id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View File

@ -48,7 +48,7 @@
android:layout_height="@dimen/element_size_normal"
android:orientation="horizontal">
<Spinner
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/account_spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"

View File

@ -21,6 +21,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:activatedBackgroundIndicator"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal">

View File

@ -28,7 +28,7 @@
android:orientation="horizontal"
android:showDividers="middle">
<Switch
<android.support.v7.widget.SwitchCompat
android:id="@android:id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>