2014-07-03 07:48:39 +02:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.preference;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2015-03-29 20:19:25 +02:00
|
|
|
import android.os.AsyncTask;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceCategory;
|
|
|
|
import android.preference.PreferenceManager;
|
2014-11-14 05:16:46 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2015-05-27 18:31:48 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
2016-01-28 06:51:51 +01:00
|
|
|
import android.widget.ImageView;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.nostra13.universalimageloader.core.assist.FailReason;
|
|
|
|
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
|
|
|
|
|
|
|
|
import org.mariotaku.twidere.Constants;
|
|
|
|
import org.mariotaku.twidere.R;
|
2014-12-04 15:31:29 +01:00
|
|
|
import org.mariotaku.twidere.model.ParcelableAccount;
|
2015-03-29 20:19:25 +02:00
|
|
|
import org.mariotaku.twidere.util.AsyncTaskUtils;
|
2015-03-24 16:40:47 +01:00
|
|
|
import org.mariotaku.twidere.util.BitmapUtils;
|
2015-03-22 18:06:25 +01:00
|
|
|
import org.mariotaku.twidere.util.MediaLoaderWrapper;
|
2015-12-31 09:32:27 +01:00
|
|
|
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-12-31 09:32:27 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2014-07-03 07:48:39 +02:00
|
|
|
public abstract class AccountsListPreference extends PreferenceCategory implements Constants {
|
|
|
|
|
2014-11-14 05:16:46 +01:00
|
|
|
private static final int[] ATTRS = {R.attr.switchKey, R.attr.switchDefault};
|
2015-05-27 18:31:48 +02:00
|
|
|
@Nullable
|
2014-11-14 05:16:46 +01:00
|
|
|
private final String mSwitchKey;
|
|
|
|
private final boolean mSwitchDefault;
|
|
|
|
|
|
|
|
public AccountsListPreference(final Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AccountsListPreference(final Context context, final AttributeSet attrs) {
|
|
|
|
this(context, attrs, android.R.attr.preferenceCategoryStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AccountsListPreference(final Context context, final AttributeSet attrs, final int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
|
|
|
|
mSwitchKey = a.getString(0);
|
|
|
|
mSwitchDefault = a.getBoolean(1, false);
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public void setAccountsData(final List<ParcelableAccount> accounts) {
|
2014-11-14 05:16:46 +01:00
|
|
|
removeAll();
|
2014-12-04 15:31:29 +01:00
|
|
|
for (final ParcelableAccount account : accounts) {
|
2015-05-27 18:31:48 +02:00
|
|
|
final AccountItemPreference preference = new AccountItemPreference(getContext(), account,
|
|
|
|
mSwitchKey, mSwitchDefault);
|
2014-11-14 05:16:46 +01:00
|
|
|
setupPreference(preference, account);
|
|
|
|
addPreference(preference);
|
|
|
|
}
|
|
|
|
final Preference preference = new Preference(getContext());
|
|
|
|
preference.setLayoutResource(R.layout.settings_layout_click_to_config);
|
|
|
|
addPreference(preference);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-15 20:40:28 +02:00
|
|
|
protected void onAttachedToHierarchy(@NonNull final PreferenceManager preferenceManager) {
|
2014-11-14 05:16:46 +01:00
|
|
|
super.onAttachedToHierarchy(preferenceManager);
|
2015-03-29 20:19:25 +02:00
|
|
|
AsyncTaskUtils.executeTask(new LoadAccountsTask(this));
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
protected abstract void setupPreference(AccountItemPreference preference, ParcelableAccount account);
|
2014-11-14 05:16:46 +01:00
|
|
|
|
|
|
|
public static final class AccountItemPreference extends Preference implements ImageLoadingListener,
|
2015-05-27 18:31:48 +02:00
|
|
|
OnSharedPreferenceChangeListener {
|
2014-11-14 05:16:46 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
private final ParcelableAccount mAccount;
|
2014-11-14 05:16:46 +01:00
|
|
|
private final SharedPreferences mSwitchPreference;
|
2015-12-31 09:32:27 +01:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
MediaLoaderWrapper mImageLoader;
|
2014-11-19 06:06:09 +01:00
|
|
|
|
2014-12-04 15:31:29 +01:00
|
|
|
public AccountItemPreference(final Context context, final ParcelableAccount account, final String switchKey,
|
2014-11-14 05:16:46 +01:00
|
|
|
final boolean switchDefault) {
|
|
|
|
super(context);
|
2015-12-31 09:32:27 +01:00
|
|
|
GeneralComponentHelper.build(context).inject(this);
|
2014-11-14 05:16:46 +01:00
|
|
|
final String switchPreferenceName = ACCOUNT_PREFERENCES_NAME_PREFIX + account.account_id;
|
|
|
|
mAccount = account;
|
|
|
|
mSwitchPreference = context.getSharedPreferences(switchPreferenceName, Context.MODE_PRIVATE);
|
|
|
|
mSwitchPreference.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadingCancelled(final String imageUri, final View view) {
|
2015-03-23 04:57:55 +01:00
|
|
|
// setIcon(R.drawable.ic_profile_image_default);
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadingComplete(final String imageUri, final View view, final Bitmap loadedImage) {
|
2015-03-24 16:40:47 +01:00
|
|
|
final Bitmap roundedBitmap = BitmapUtils.getCircleBitmap(loadedImage);
|
2014-11-19 06:06:09 +01:00
|
|
|
setIcon(new BitmapDrawable(getContext().getResources(), roundedBitmap));
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadingFailed(final String imageUri, final View view, final FailReason failReason) {
|
2015-03-23 04:57:55 +01:00
|
|
|
// setIcon(R.drawable.ic_profile_image_default);
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadingStarted(final String imageUri, final View view) {
|
2015-03-23 04:57:55 +01:00
|
|
|
// setIcon(R.drawable.ic_profile_image_default);
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToHierarchy(@NonNull final PreferenceManager preferenceManager) {
|
|
|
|
super.onAttachedToHierarchy(preferenceManager);
|
|
|
|
setTitle(mAccount.name);
|
|
|
|
setSummary(String.format("@%s", mAccount.screen_name));
|
2015-03-23 04:57:55 +01:00
|
|
|
// setIcon(R.drawable.ic_profile_image_default);
|
2014-11-19 06:06:09 +01:00
|
|
|
mImageLoader.loadProfileImage(mAccount.profile_image_url, this);
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBindView(@NonNull final View view) {
|
|
|
|
super.onBindView(view);
|
2016-01-28 06:51:51 +01:00
|
|
|
final View iconView = view.findViewById(android.R.id.icon);
|
|
|
|
if (iconView instanceof ImageView) {
|
|
|
|
final ImageView imageView = (ImageView) iconView;
|
|
|
|
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
|
|
|
}
|
2014-11-14 05:16:46 +01:00
|
|
|
final View titleView = view.findViewById(android.R.id.title);
|
|
|
|
if (titleView instanceof TextView) {
|
|
|
|
((TextView) titleView).setSingleLine(true);
|
|
|
|
}
|
|
|
|
final View summaryView = view.findViewById(android.R.id.summary);
|
|
|
|
if (summaryView instanceof TextView) {
|
|
|
|
((TextView) summaryView).setSingleLine(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:50:32 +02:00
|
|
|
private static class LoadAccountsTask extends AsyncTask<Object, Object, List<ParcelableAccount>> {
|
2014-11-14 05:16:46 +01:00
|
|
|
|
|
|
|
private final AccountsListPreference mPreference;
|
|
|
|
|
|
|
|
public LoadAccountsTask(final AccountsListPreference preference) {
|
|
|
|
mPreference = preference;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-09 13:50:32 +02:00
|
|
|
protected List<ParcelableAccount> doInBackground(final Object... params) {
|
2014-12-04 15:31:29 +01:00
|
|
|
return ParcelableAccount.getAccountsList(mPreference.getContext(), false);
|
2014-11-14 05:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-04 15:31:29 +01:00
|
|
|
protected void onPostExecute(final List<ParcelableAccount> result) {
|
2014-11-14 05:16:46 +01:00
|
|
|
mPreference.setAccountsData(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
}
|