fixed activity item

added load default api configs from github repo
This commit is contained in:
Mariotaku Lee 2016-04-04 14:23:44 +08:00
parent dcd0f008d9
commit 962c682e37
7 changed files with 164 additions and 35 deletions

View File

@ -26,8 +26,12 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
@ -35,17 +39,32 @@ import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.materialdialogs.AlertDialogWrapper;
import org.mariotaku.restfu.annotation.method.GET;
import org.mariotaku.restfu.http.HttpRequest;
import org.mariotaku.restfu.http.HttpResponse;
import org.mariotaku.restfu.http.RestHttpClient;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.adapter.ArrayAdapter;
import org.mariotaku.twidere.fragment.BaseSupportDialogFragment;
import org.mariotaku.twidere.model.CustomAPIConfig;
import org.mariotaku.twidere.model.ParcelableCredentials;
import org.mariotaku.twidere.provider.TwidereDataStore.Accounts;
import org.mariotaku.twidere.util.JsonSerializer;
import org.mariotaku.twidere.util.ParseUtils;
import org.mariotaku.twidere.util.TwitterAPIFactory;
import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.inject.Inject;
import static org.mariotaku.twidere.util.Utils.getNonEmptyString;
import static org.mariotaku.twidere.util.Utils.trim;
@ -264,26 +283,91 @@ public class APIEditorActivity extends BaseActivity implements OnCheckedChangeLi
}
public static class LoadDefaultsChooserDialogFragment extends BaseSupportDialogFragment
implements DialogInterface.OnClickListener {
private CustomAPIConfig[] mAPIConfigs;
implements DialogInterface.OnClickListener, LoaderManager.LoaderCallbacks<List<CustomAPIConfig>> {
private ArrayAdapter<CustomAPIConfig> mAdapter;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getContext();
mAPIConfigs = CustomAPIConfig.listDefault(context);
List<CustomAPIConfig> configs = CustomAPIConfig.listDefault(context);
mAdapter = new CustomAPIConfigArrayAdapter(context, configs);
final AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
String[] entries = new String[mAPIConfigs.length];
for (int i = 0, mAPIConfigsLength = mAPIConfigs.length; i < mAPIConfigsLength; i++) {
entries[i] = mAPIConfigs[i].getLocalizedName(context);
}
builder.setItems(entries, this);
builder.setAdapter(mAdapter, this);
getLoaderManager().initLoader(0, null, this);
return builder.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
((APIEditorActivity) getActivity()).setAPIConfig(mAPIConfigs[which]);
((APIEditorActivity) getActivity()).setAPIConfig(mAdapter.getItem(which));
}
@Override
public Loader<List<CustomAPIConfig>> onCreateLoader(int id, Bundle args) {
return new DefaultAPIConfigLoader(getContext());
}
@Override
public void onLoadFinished(Loader<List<CustomAPIConfig>> loader, List<CustomAPIConfig> data) {
if (data != null) {
mAdapter.clear();
mAdapter.addAll(data);
}
}
@Override
public void onLoaderReset(Loader<List<CustomAPIConfig>> loader) {
}
public static class DefaultAPIConfigLoader extends AsyncTaskLoader<List<CustomAPIConfig>> {
public static final String DEFAULT_API_CONFIGS_URL = "https://raw.githubusercontent.com/TwidereProject/Twidere-Android/master/twidere/src/main/assets/data/default_api_configs.json";
@Inject
RestHttpClient mClient;
public DefaultAPIConfigLoader(Context context) {
super(context);
GeneralComponentHelper.build(context).inject(this);
}
@Override
public List<CustomAPIConfig> loadInBackground() {
HttpRequest request = new HttpRequest(GET.METHOD, DEFAULT_API_CONFIGS_URL,
null, null, null);
HttpResponse response = null;
try {
response = mClient.newCall(request).execute();
if (response.isSuccessful()) {
final InputStream is = response.getBody().stream();
return JsonSerializer.parseList(is, CustomAPIConfig.class);
}
} catch (IOException e) {
// Ignore
} finally {
Utils.closeSilently(response);
}
return null;
}
@Override
protected void onStartLoading() {
forceLoad();
}
}
private class CustomAPIConfigArrayAdapter extends ArrayAdapter<CustomAPIConfig> {
public CustomAPIConfigArrayAdapter(Context context, List<CustomAPIConfig> defaultItems) {
super(context, R.layout.md_listitem, defaultItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
CustomAPIConfig config = getItem(position);
((TextView) view.findViewById(com.afollestad.materialdialogs.R.id.title)).setText(config.getLocalizedName(getContext()));
return view;
}
}
}
}

View File

@ -28,6 +28,7 @@ import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
@ -237,10 +238,13 @@ public class AccountSelectorActivity extends BaseActivity implements
return intent.getBooleanExtra(EXTRA_SELECT_ONLY_ITEM, false);
}
@Nullable
private Intent getStartIntent() {
final Intent intent = getIntent();
final Intent startIntent = intent.getParcelableExtra(EXTRA_START_INTENT);
startIntent.setExtrasClassLoader(TwidereApplication.class.getClassLoader());
if (startIntent != null) {
startIntent.setExtrasClassLoader(TwidereApplication.class.getClassLoader());
}
return startIntent;
}

View File

@ -208,7 +208,7 @@ public class BaseActivity extends ATEActivity implements Constants, IExtendedAct
intentFilter.addDataAuthority("fanfou.com", null);
try {
adapter.enableForegroundDispatch(this, intent, new IntentFilter[]{intentFilter}, null);
} catch (SecurityException e) {
} catch (Exception e) {
// Ignore if blocked by modified roms
}
}
@ -220,7 +220,7 @@ public class BaseActivity extends ATEActivity implements Constants, IExtendedAct
if (adapter != null && adapter.isEnabled()) {
try {
adapter.disableForegroundDispatch(this);
} catch (SecurityException e) {
} catch (Exception e) {
// Ignore if blocked by modified roms
}
}

View File

@ -8,6 +8,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;
@ -80,16 +81,9 @@ public class ActivityTitleSummaryMessage {
R.string.activity_about_me_like_multi, sources, nameFirst);
}
}
final StringBuilder summaryBuilder = new StringBuilder();
boolean first = true;
for (ParcelableStatus status : activity.target_statuses) {
if (!first) {
summaryBuilder.append('\n');
}
summaryBuilder.append(status.text_unescaped.replace('\n', ' '));
first = false;
}
return new ActivityTitleSummaryMessage(typeIcon, color, title, summaryBuilder.toString());
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.RETWEET: {
int typeIcon = R.drawable.ic_activity_action_retweet;
@ -102,7 +96,8 @@ public class ActivityTitleSummaryMessage {
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweet,
R.string.activity_about_me_retweet_multi, sources, nameFirst);
}
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context,
activity.target_object_statuses, nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.FAVORITED_RETWEET: {
@ -121,7 +116,8 @@ public class ActivityTitleSummaryMessage {
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_retweet,
R.string.activity_about_me_liked_retweet_multi, sources, nameFirst);
}
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.RETWEETED_RETWEET: {
@ -130,7 +126,8 @@ public class ActivityTitleSummaryMessage {
int color = ContextCompat.getColor(context, R.color.highlight_retweet);
CharSequence title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_retweet,
R.string.activity_about_me_retweeted_retweet_multi, sources, nameFirst);
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.RETWEETED_MENTION: {
@ -139,7 +136,8 @@ public class ActivityTitleSummaryMessage {
int color = ContextCompat.getColor(context, R.color.highlight_retweet);
CharSequence title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_mention,
R.string.activity_about_me_retweeted_mention_multi, sources, nameFirst);
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.FAVORITED_MENTION: {
@ -158,7 +156,8 @@ public class ActivityTitleSummaryMessage {
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_mention,
R.string.activity_about_me_liked_mention_multi, sources, nameFirst);
}
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.LIST_CREATED: {
@ -225,7 +224,8 @@ public class ActivityTitleSummaryMessage {
CharSequence title;
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_media_tagged,
R.string.activity_about_me_media_tagged_multi, sources, nameFirst);
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.FAVORITED_MEDIA_TAGGED: {
@ -244,7 +244,8 @@ public class ActivityTitleSummaryMessage {
title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_liked_media_tagged,
R.string.activity_about_me_liked_media_tagged_multi, sources, nameFirst);
}
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
case Activity.Action.RETWEETED_MEDIA_TAGGED: {
@ -253,13 +254,33 @@ public class ActivityTitleSummaryMessage {
int color = ContextCompat.getColor(context, R.color.highlight_retweet);
CharSequence title = getTitleStringAboutMe(resources, manager, R.string.activity_about_me_retweeted_media_tagged,
R.string.activity_about_me_retweeted_media_tagged_multi, sources, nameFirst);
final String summary = activity.target_statuses[0].text_unescaped;
final Spanned summary = generateStatusTextSummary(context, activity.target_statuses,
nameFirst);
return new ActivityTitleSummaryMessage(typeIcon, color, title, summary);
}
}
return null;
}
public static Spanned generateStatusTextSummary(Context context, ParcelableStatus[] statuses, boolean nameFirst) {
if (statuses == null) return null;
final SpannableStringBuilder summaryBuilder = new SpannableStringBuilder();
boolean first = true;
for (ParcelableStatus status : statuses) {
if (!first) {
summaryBuilder.append('\n');
}
final SpannableString displayName = new SpannableString(UserColorNameManager.decideDisplayName(status.user_nickname,
status.user_name, status.user_screen_name, nameFirst));
displayName.setSpan(new StyleSpan(Typeface.BOLD), 0, displayName.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
summaryBuilder.append(SpanFormatter.format(context.getString(R.string.title_summary_line_format),
displayName, status.text_unescaped.replace('\n', ' ')));
first = false;
}
return summaryBuilder;
}
private static Spanned getTitleStringAboutMe(Resources resources, UserColorNameManager manager,
int stringRes, int stringResMulti,
ParcelableUser[] sources, boolean nameFirst) {

View File

@ -16,6 +16,7 @@ import org.mariotaku.twidere.util.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
/**
@ -95,14 +96,14 @@ public final class CustomAPIConfig implements Constants {
}
@NonNull
public static CustomAPIConfig[] listDefault(@NonNull Context context) {
public static List<CustomAPIConfig> listDefault(@NonNull Context context) {
final AssetManager assets = context.getAssets();
InputStream is = null;
try {
is = assets.open("data/default_api_configs.json");
List<CustomAPIConfig> configList = JsonSerializer.parseList(is, CustomAPIConfig.class);
if (configList == null) return listBuiltin(context);
return configList.toArray(new CustomAPIConfig[configList.size()]);
return configList;
} catch (IOException e) {
return listBuiltin(context);
} finally {
@ -110,10 +111,10 @@ public final class CustomAPIConfig implements Constants {
}
}
public static CustomAPIConfig[] listBuiltin(@NonNull Context context) {
return new CustomAPIConfig[]{new CustomAPIConfig(context.getString(R.string.provider_default),
public static List<CustomAPIConfig> listBuiltin(@NonNull Context context) {
return Collections.singletonList(new CustomAPIConfig(context.getString(R.string.provider_default),
DEFAULT_TWITTER_API_URL_FORMAT, ParcelableCredentials.AuthType.OAUTH, true, false,
TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)};
TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET));
}
static class AuthTypeConverter extends StringBasedTypeConverter<Integer> {

View File

@ -21,6 +21,7 @@ package org.mariotaku.twidere.util.dagger;
import android.support.v7.widget.RecyclerView;
import org.mariotaku.twidere.activity.APIEditorActivity;
import org.mariotaku.twidere.activity.BaseActivity;
import org.mariotaku.twidere.activity.ComposeActivity;
import org.mariotaku.twidere.activity.MediaViewerActivity;
@ -147,4 +148,6 @@ public interface GeneralComponent {
void inject(UpdateProfileBannerImageTask<Object> task);
void inject(AsyncTwitterWrapper.UpdateProfileImageTask<Object> task);
void inject(APIEditorActivity.LoadDefaultsChooserDialogFragment.DefaultAPIConfigLoader loader);
}

View File

@ -0,0 +1,16 @@
package org.mariotaku.twidere.util.support;
import android.text.SpannableStringBuilder;
/**
* Created by mariotaku on 16/4/4.
*/
public class SpannableStringBuilderSupport {
public static void append(SpannableStringBuilder builder, CharSequence text, Object span, int flags) {
int start = builder.length();
builder.append(text);
builder.setSpan(span, start, builder.length(), flags);
}
}