use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class SettingsFragment method onActivityCreated.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String preferenceRoot = getArguments() != null ? getArguments().getString(ARG_PREFERENCE_ROOT, null) : null;
if (preferenceRoot == null) {
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(getContext());
AccessToken[] accessTokens = helper.getAccessTokens();
CharSequence[] entries = new CharSequence[accessTokens.length + 1];
CharSequence[] entryValues = new CharSequence[accessTokens.length + 1];
for (int i = 0; i < accessTokens.length; i++) {
AccessToken accessToken = accessTokens[i];
entries[i] = TwitterStringUtils.plusAtMark(accessToken.getScreenName(), accessToken.getUrl());
entryValues[i] = accessToken.getKeyString();
}
entries[entries.length - 1] = getString(R.string.add_account);
entryValues[entryValues.length - 1] = "-1";
ListPreference nowAccountList = (ListPreference) findPreference("AccountKey");
nowAccountList.setEntries(entries);
nowAccountList.setEntryValues(entryValues);
nowAccountList.setDefaultValue(defaultSharedPreferences.getString("AccountKey", "-1"));
nowAccountList.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.equals("-1")) {
GlobalApplication.twitter = null;
startActivity(new Intent(getContext(), OAuthActivity.class));
} else {
TokenSQLiteOpenHelper tokenOpenHelper = new TokenSQLiteOpenHelper(this.getContext());
AccessToken accessToken = tokenOpenHelper.getAccessToken((String) newValue);
tokenOpenHelper.close();
((GlobalApplication) getActivity().getApplication()).initTwitter(accessToken);
startActivity(new Intent(getContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
}
return true;
});
findPreference("logout").setOnPreferenceClickListener(preference -> {
new AlertDialog.Builder(getContext()).setMessage(R.string.confirm_logout).setCancelable(true).setPositiveButton(R.string.do_logout, (dialog, i) -> {
helper.deleteAccessToken(helper.getAccessToken(defaultSharedPreferences.getString("AccountKey", "-1")));
int point = helper.getSize() - 1;
if (point != -1) {
AccessToken accessToken = helper.getAccessTokens()[point];
defaultSharedPreferences.edit().putString("AccountKey", accessToken.getKeyString()).apply();
((GlobalApplication) getActivity().getApplication()).initTwitter(accessToken);
startActivity(new Intent(getContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
} else {
defaultSharedPreferences.edit().putString("AccountKey", "-1").apply();
GlobalApplication.twitter = null;
startActivity(new Intent(getContext(), OAuthActivity.class));
}
}).setNeutralButton(R.string.back, (dialog, i) -> dialog.cancel()).show();
return false;
});
ListPreference nowThemeMode = (ListPreference) findPreference("nightModeType");
nowThemeMode.setOnPreferenceChangeListener((preference, newValue) -> {
switch(String.valueOf(newValue)) {
case "mode_night_no":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case "mode_night_auto":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
case "mode_night_follow_system":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
case "mode_night_yes":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
default:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return true;
});
Preference causeError = findPreference("cause_error");
causeError.setOnPreferenceClickListener(preference -> {
getActivity().finish();
throw new NullPointerException();
});
Preference licenseThisApp = findPreference("license_at_this_app");
licenseThisApp.setOnPreferenceClickListener(preference -> {
getContext().startActivity(new Intent(getContext(), LicensesActivity.class).putExtra("title", getResources().getText(R.string.app_name)).putExtra("library_name", "twicalico"));
return true;
});
Preference sourceCodeLink = findPreference("source_code_link");
sourceCodeLink.setOnPreferenceClickListener(preference -> {
AppCustomTabsKt.launchChromeCustomTabs(getContext(), "https://github.com/moko256/twicalico");
return false;
});
Preference version = findPreference("app_version");
version.setSummary(BuildConfig.VERSION_NAME);
version.setOnPreferenceClickListener(preference -> {
Date birthday = new Date(1446956982000L);
Toast.makeText(getContext(), getString(R.string.birthday_of_this_app_is, birthday) + "\n\n" + getString(R.string.age_of_this_app_is, (int) Math.floor((new Date().getTime() - birthday.getTime()) / (31557600000L))), Toast.LENGTH_LONG).show();
return false;
});
} else if (preferenceRoot.equals("license")) {
PreferenceScreen license = getPreferenceScreen();
for (int i = 0, length = license.getPreferenceCount(); i < length; i++) {
license.getPreference(i).setOnPreferenceClickListener(preference -> {
getContext().startActivity(new Intent(getContext(), LicensesActivity.class).putExtra("title", preference.getTitle()).putExtra("library_name", // "license_lib_".length
preference.getKey().substring(12)));
return true;
});
}
}
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class TokenSQLiteOpenHelper method getAccessTokens.
public AccessToken[] getAccessTokens() {
SQLiteDatabase database = getReadableDatabase();
Cursor c = database.query(TABLE_NAME, TABLE_COLUMNS, null, null, null, null, null);
AccessToken[] accessTokens = new AccessToken[c.getCount()];
while (c.moveToNext()) {
accessTokens[c.getPosition()] = convertFromCursor(c);
}
c.close();
database.close();
return accessTokens;
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class TokenSQLiteOpenHelper method getAccessToken.
public AccessToken getAccessToken(String key) {
Pair<String, Long> pair = AccessTokenKt.splitAccessTokenKey(key);
SQLiteDatabase database = getReadableDatabase();
Cursor c = database.query(TABLE_NAME, TABLE_COLUMNS, "url = '" + pair.getFirst() + "' AND " + "userId = " + String.valueOf(pair.getSecond()), null, null, null, null, "1");
AccessToken accessToken;
if (c.moveToNext()) {
accessToken = convertFromCursor(c);
} else {
accessToken = null;
}
c.close();
database.close();
return accessToken;
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class GlobalApplication method getTwitterInstance.
@NonNull
public Twitter getTwitterInstance(@NonNull AccessToken accessToken) {
Twitter t;
Configuration conf;
if (accessToken.getType() == Type.TWITTER) {
conf = new ConfigurationBuilder().setTweetModeExtended(true).setOAuthConsumerKey(BuildConfig.CONSUMER_KEY).setOAuthConsumerSecret(BuildConfig.CONSUMER_SECRET).setOAuthAccessToken(accessToken.getToken()).setOAuthAccessTokenSecret(accessToken.getTokenSecret()).build();
t = twitterCache.get(conf);
if (t == null) {
t = new TwitterFactory(conf).getInstance();
twitterCache.put(conf, t);
}
} else {
conf = new ConfigurationBuilder().setOAuthAccessToken(accessToken.getToken()).setRestBaseURL(accessToken.getUrl()).build();
t = twitterCache.get(conf);
if (t == null) {
t = new MastodonTwitterImpl(conf, accessToken.getUserId(), getOkHttpClient(conf.getHttpClientConfiguration()).newBuilder());
twitterCache.put(conf, t);
}
}
return t;
}
use of com.github.moko256.latte.client.base.entity.AccessToken in project twicalico by moko256.
the class GlobalApplication method onCreate.
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("crash_log", getString(R.string.crash_log), NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(getString(R.string.crash_log_channel_description));
channel.setLightColor(Color.RED);
channel.enableLights(true);
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
final Thread.UncaughtExceptionHandler defaultUnCaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
try {
new ExceptionNotification().create(e, getApplicationContext());
} catch (Throwable fe) {
fe.printStackTrace();
} finally {
defaultUnCaughtExceptionHandler.uncaughtException(t, e);
}
});
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
configuration = new AppConfiguration();
configuration.isPatternTweetMuteEnabled = defaultSharedPreferences.getBoolean("patternTweetMuteEnabled", false);
if (configuration.isPatternTweetMuteEnabled) {
try {
configuration.tweetMutePattern = Pattern.compile(defaultSharedPreferences.getString("tweetMutePattern", ""));
} catch (PatternSyntaxException e) {
e.printStackTrace();
configuration.isPatternTweetMuteEnabled = false;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
configuration.isPatternTweetMuteShowOnlyImageEnabled = defaultSharedPreferences.getBoolean("patternTweetMuteShowOnlyImageEnabled", false);
if (configuration.isPatternTweetMuteShowOnlyImageEnabled) {
try {
configuration.tweetMuteShowOnlyImagePattern = Pattern.compile(defaultSharedPreferences.getString("tweetMuteShowOnlyImagePattern", ""));
} catch (PatternSyntaxException e) {
e.printStackTrace();
configuration.isPatternTweetMuteShowOnlyImageEnabled = false;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
configuration.isPatternUserScreenNameMuteEnabled = defaultSharedPreferences.getBoolean("patternUserScreenNameMuteEnabled", false);
if (configuration.isPatternUserScreenNameMuteEnabled) {
try {
configuration.userScreenNameMutePattern = Pattern.compile(defaultSharedPreferences.getString("userScreenNameMutePattern", ""));
} catch (PatternSyntaxException e) {
e.printStackTrace();
configuration.isPatternUserScreenNameMuteEnabled = false;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
configuration.isPatternUserNameMuteEnabled = defaultSharedPreferences.getBoolean("patternUserNameMuteEnabled", false);
if (configuration.isPatternUserNameMuteEnabled) {
try {
configuration.userNameMutePattern = Pattern.compile(defaultSharedPreferences.getString("userNameMutePattern", ""));
} catch (PatternSyntaxException e) {
e.printStackTrace();
configuration.isPatternUserNameMuteEnabled = false;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
configuration.isPatternTweetSourceMuteEnabled = defaultSharedPreferences.getBoolean("patternTweetSourceMuteEnabled", false);
if (configuration.isPatternTweetSourceMuteEnabled) {
try {
configuration.tweetSourceMutePattern = Pattern.compile(defaultSharedPreferences.getString("tweetSourceMutePattern", ""));
} catch (PatternSyntaxException e) {
e.printStackTrace();
configuration.isPatternTweetSourceMuteEnabled = false;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
configuration.isTimelineImageLoad = Boolean.valueOf(defaultSharedPreferences.getString("isTimelineImageLoad", "true"));
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
@AppCompatDelegate.NightMode int mode = AppCompatDelegate.MODE_NIGHT_NO;
switch(defaultSharedPreferences.getString("nightModeType", "mode_night_no_value")) {
case "mode_night_no":
mode = AppCompatDelegate.MODE_NIGHT_NO;
break;
case "mode_night_auto":
mode = AppCompatDelegate.MODE_NIGHT_AUTO;
break;
case "mode_night_follow_system":
mode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
break;
case "mode_night_yes":
mode = AppCompatDelegate.MODE_NIGHT_YES;
break;
}
AppCompatDelegate.setDefaultNightMode(mode);
String accountKey = defaultSharedPreferences.getString("AccountKey", "-1");
if (accountKey.equals("-1"))
return;
TokenSQLiteOpenHelper tokenOpenHelper = new TokenSQLiteOpenHelper(this);
AccessToken accessToken = tokenOpenHelper.getAccessToken(accountKey);
tokenOpenHelper.close();
if (accessToken == null)
return;
initTwitter(accessToken);
super.onCreate();
}
Aggregations