use of com.github.moko256.twicalico.database.TokenSQLiteOpenHelper 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.twicalico.database.TokenSQLiteOpenHelper 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();
}
use of com.github.moko256.twicalico.database.TokenSQLiteOpenHelper in project twicalico by moko256.
the class MainActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subscription = new CompositeSubscription();
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.getChildAt(0).setOnClickListener(v -> {
Fragment fragment = getMainFragment();
if (fragment instanceof MovableTopInterface) {
((MovableTopInterface) fragment).moveToTop();
}
});
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
if (isDrawerAccountsSelection) {
changeIsDrawerAccountsSelection();
}
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
if (!item.isChecked()) {
switch(id) {
case R.id.nav_timeline:
replaceFragment(new HomeTimeLineFragment());
break;
case R.id.nav_mentions:
replaceFragment(new MentionsFragment());
break;
case R.id.nav_account:
startMyUserActivity();
break;
case R.id.nav_follow_and_follower:
replaceFragment(new MyFollowFollowerFragment());
break;
case R.id.nav_like:
replaceFragment(UserLikeFragment.newInstance(GlobalApplication.userId));
break;
case R.id.nav_settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
}
}
drawer.closeDrawer(GravityCompat.START);
return (id != R.id.nav_settings) && (id != R.id.nav_account);
});
headerView = navigationView.inflateHeaderView(R.layout.nav_header_main);
userNameText = headerView.findViewById(R.id.user_name);
userIdText = headerView.findViewById(R.id.user_id);
userImage = headerView.findViewById(R.id.user_image);
userBackgroundImage = headerView.findViewById(R.id.user_bg_image);
userBackgroundImage.setOnClickListener(v -> changeIsDrawerAccountsSelection());
updateDrawerImage();
accountListView = new RecyclerView(this);
accountListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
accountListView.setLayoutManager(new LinearLayoutManager(this));
accountListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
accountListView.setVisibility(View.GONE);
navigationView.addHeaderView(accountListView);
SelectAccountsAdapter adapter = new SelectAccountsAdapter(this);
adapter.setOnImageButtonClickListener(accessToken -> {
drawer.closeDrawer(GravityCompat.START);
if (accessToken.getUserId() != GlobalApplication.userId) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", accessToken.getKeyString()).apply();
((GlobalApplication) getApplication()).initTwitter(accessToken);
updateDrawerImage();
clearAndPrepareFragment();
}
});
adapter.setOnAddButtonClickListener(v -> {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("AccountKey", "-1").apply();
GlobalApplication.twitter = null;
startActivity(new Intent(this, OAuthActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
});
adapter.setOnRemoveButtonClickListener(v -> new AlertDialog.Builder(this).setMessage(R.string.confirm_logout).setCancelable(true).setPositiveButton(R.string.do_logout, (dialog, i) -> {
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
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) getApplication()).initTwitter(accessToken);
updateDrawerImage();
clearAndPrepareFragment();
} else {
adapter.getOnAddButtonClickListener().onClick(v);
}
helper.close();
}).setNeutralButton(R.string.back, (dialog, i) -> dialog.cancel()).show());
accountListView.setAdapter(adapter);
subscription.add(Single.create(singleSubscriber -> {
TokenSQLiteOpenHelper helper = new TokenSQLiteOpenHelper(this);
AccessToken[] accessTokens = helper.getAccessTokens();
helper.close();
ArrayList<Pair<User, AccessToken>> r = new ArrayList<>(accessTokens.length);
for (AccessToken accessToken : accessTokens) {
long id = accessToken.getUserId();
CachedUsersSQLiteOpenHelper userHelper = new CachedUsersSQLiteOpenHelper(this, id, accessToken.getType() == Type.TWITTER);
User user = userHelper.getCachedUser(id);
if (user == null) {
try {
user = ((GlobalApplication) getApplication()).getTwitterInstance(accessToken).verifyCredentials();
userHelper.addCachedUser(user);
} catch (TwitterException e) {
singleSubscriber.onError(e);
return;
} finally {
userHelper.close();
}
}
r.add(new Pair<>(user, accessToken));
}
singleSubscriber.onSuccess(r);
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
ArrayList<Pair<User, AccessToken>> pairs = (ArrayList<Pair<User, AccessToken>>) o;
adapter.getImagesList().addAll(pairs);
adapter.notifyDataSetChanged();
}, Throwable::printStackTrace));
findViewById(R.id.fab).setOnClickListener(v -> startActivity(new Intent(this, PostActivity.class)));
tabLayout = findViewById(R.id.toolbar_tab);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment = ((FragmentPagerAdapter) ((UseTabsInterface) getMainFragment()).getTabsViewPager().getAdapter()).getFragment(tab.getPosition());
if (fragment instanceof MovableTopInterface) {
((MovableTopInterface) fragment).moveToTop();
}
}
});
tweetListViewPool = new RecyclerView.RecycledViewPool();
userListViewPool = new RecyclerView.RecycledViewPool();
getSupportFragmentManager().addOnBackStackChangedListener(() -> attachFragment(getMainFragment()));
if (savedInstanceState == null) {
prepareFragment();
}
}
use of com.github.moko256.twicalico.database.TokenSQLiteOpenHelper in project twicalico by moko256.
the class OAuthActivity method storeAccessToken.
private void storeAccessToken(AccessToken accessToken) {
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
TokenSQLiteOpenHelper tokenOpenHelper = new TokenSQLiteOpenHelper(this);
tokenOpenHelper.addAccessToken(accessToken);
tokenOpenHelper.close();
defaultSharedPreferences.edit().putString("AccountKey", accessToken.getKeyString()).apply();
((GlobalApplication) getApplication()).initTwitter(accessToken);
startActivity(new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
}
Aggregations