Search in sources :

Example 1 with AccountManager

use of com.keylesspalace.tusky.db.AccountManager in project Tusky by tuskyapp.

the class PreferencesFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    accountManager = TuskyApplication.getInstance(getActivity()).getServiceLocator().get(AccountManager.class);
    int preference = getArguments().getInt("preference");
    addPreferencesFromResource(preference);
    Preference notificationPreferences = findPreference("notificationPreferences");
    if (notificationPreferences != null) {
        AccountEntity activeAccount = accountManager.getActiveAccount();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && activeAccount != null) {
            notificationPreferences.setSummary(getString(R.string.pref_summary_notifications, activeAccount.getFullName()));
        }
        // on Android O and newer, launch the system notification settings instead of the app settings
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationPreferences.setOnPreferenceClickListener(pref -> {
                Intent intent = new Intent();
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("android.provider.extra.APP_PACKAGE", BuildConfig.APPLICATION_ID);
                startActivity(intent);
                return true;
            });
        } else {
            notificationPreferences.setOnPreferenceClickListener(pref -> {
                PreferencesActivity activity = (PreferencesActivity) getActivity();
                if (activity != null) {
                    activity.showFragment(R.xml.notification_preferences, R.string.pref_title_edit_notification_settings);
                }
                return true;
            });
        }
    }
    Preference timelineFilterPreferences = findPreference("timelineFilterPreferences");
    if (timelineFilterPreferences != null) {
        timelineFilterPreferences.setOnPreferenceClickListener(pref -> {
            PreferencesActivity activity = (PreferencesActivity) getActivity();
            if (activity != null) {
                activity.showFragment(R.xml.timeline_filter_preferences, R.string.pref_title_status_tabs);
            }
            return true;
        });
    }
    Preference httpProxyPreferences = findPreference("httpProxyPreferences");
    if (httpProxyPreferences != null) {
        httpProxyPreferences.setOnPreferenceClickListener(pref -> {
            PreferencesActivity activity = (PreferencesActivity) getActivity();
            if (activity != null) {
                pendingRestart = false;
                activity.showFragment(R.xml.http_proxy_preferences, R.string.pref_title_http_proxy_settings);
            }
            return true;
        });
    }
    if (preference == R.xml.notification_preferences) {
        AccountEntity activeAccount = accountManager.getActiveAccount();
        if (activeAccount != null) {
            CheckBoxPreference notificationPref = (CheckBoxPreference) findPreference("notificationsEnabled");
            notificationPref.setChecked(activeAccount.getNotificationsEnabled());
            CheckBoxPreference mentionedPref = (CheckBoxPreference) findPreference("notificationFilterMentions");
            mentionedPref.setChecked(activeAccount.getNotificationsMentioned());
            CheckBoxPreference followedPref = (CheckBoxPreference) findPreference("notificationFilterFollows");
            followedPref.setChecked(activeAccount.getNotificationsFollowed());
            CheckBoxPreference boostedPref = (CheckBoxPreference) findPreference("notificationFilterReblogs");
            boostedPref.setChecked(activeAccount.getNotificationsReblogged());
            CheckBoxPreference favoritedPref = (CheckBoxPreference) findPreference("notificationFilterFavourites");
            favoritedPref.setChecked(activeAccount.getNotificationsFavorited());
            CheckBoxPreference soundPref = (CheckBoxPreference) findPreference("notificationAlertSound");
            soundPref.setChecked(activeAccount.getNotificationSound());
            CheckBoxPreference vibrationPref = (CheckBoxPreference) findPreference("notificationAlertVibrate");
            vibrationPref.setChecked(activeAccount.getNotificationVibration());
            CheckBoxPreference lightPref = (CheckBoxPreference) findPreference("notificationAlertLight");
            lightPref.setChecked(activeAccount.getNotificationLight());
        }
    }
}
Also used : CheckBoxPreference(android.preference.CheckBoxPreference) EditTextPreference(android.preference.EditTextPreference) Preference(android.preference.Preference) PreferencesActivity(com.keylesspalace.tusky.PreferencesActivity) CheckBoxPreference(android.preference.CheckBoxPreference) AccountManager(com.keylesspalace.tusky.db.AccountManager) Intent(android.content.Intent) AccountEntity(com.keylesspalace.tusky.db.AccountEntity)

Example 2 with AccountManager

use of com.keylesspalace.tusky.db.AccountManager in project Tusky by tuskyapp.

the class NotificationsFragment method saveNewestNotificationId.

private void saveNewestNotificationId(List<Notification> notifications) {
    AccountManager accountManager = TuskyApplication.getInstance(getContext()).getServiceLocator().get(AccountManager.class);
    AccountEntity account = accountManager.getActiveAccount();
    BigInteger lastNoti = new BigInteger(account.getLastNotificationId());
    for (Notification noti : notifications) {
        BigInteger a = new BigInteger(noti.getId());
        if (isBiggerThan(a, lastNoti)) {
            lastNoti = a;
        }
    }
    Log.d(TAG, "saving newest noti id: " + lastNoti);
    account.setLastNotificationId(lastNoti.toString());
    accountManager.saveAccount(account);
}
Also used : BigInteger(java.math.BigInteger) AccountManager(com.keylesspalace.tusky.db.AccountManager) AccountEntity(com.keylesspalace.tusky.db.AccountEntity) Notification(com.keylesspalace.tusky.entity.Notification)

Example 3 with AccountManager

use of com.keylesspalace.tusky.db.AccountManager in project Tusky by tuskyapp.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    int tabPosition = 0;
    accountManager = TuskyApplication.getInstance(this).getServiceLocator().get(AccountManager.class);
    if (intent != null) {
        long accountId = intent.getLongExtra(NotificationHelper.ACCOUNT_ID, -1);
        if (accountId != -1) {
            // user clicked a notification, show notification tab and switch user if necessary
            tabPosition = 1;
            AccountEntity account = accountManager.getActiveAccount();
            if (account == null || accountId != account.getId()) {
                accountManager.setActiveAccount(accountId);
            }
        }
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FloatingActionButton floatingBtn = findViewById(R.id.floating_btn);
    ImageButton drawerToggle = findViewById(R.id.drawer_toggle);
    TabLayout tabLayout = findViewById(R.id.tab_layout);
    viewPager = findViewById(R.id.pager);
    floatingBtn.setOnClickListener(v -> {
        Intent composeIntent = new Intent(getApplicationContext(), ComposeActivity.class);
        startActivityForResult(composeIntent, COMPOSE_RESULT);
    });
    setupDrawer();
    // Setup the navigation drawer toggle button.
    ThemeUtils.setDrawableTint(this, drawerToggle.getDrawable(), R.attr.toolbar_icon_tint);
    drawerToggle.setOnClickListener(v -> drawer.openDrawer());
    /* Fetch user info while we're doing other things. This has to be done after setting up the
         * drawer, though, because its callback touches the header in the drawer. */
    fetchUserInfo();
    // Setup the tabs and timeline pager.
    TimelinePagerAdapter adapter = new TimelinePagerAdapter(getSupportFragmentManager());
    int pageMargin = getResources().getDimensionPixelSize(R.dimen.tab_page_margin);
    viewPager.setPageMargin(pageMargin);
    Drawable pageMarginDrawable = ThemeUtils.getDrawable(this, R.attr.tab_page_margin_drawable, R.drawable.tab_page_margin_dark);
    viewPager.setPageMarginDrawable(pageMarginDrawable);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    int[] tabIcons = { R.drawable.ic_home_24dp, R.drawable.ic_notifications_24dp, R.drawable.ic_local_24dp, R.drawable.ic_public_24dp };
    String[] pageTitles = { getString(R.string.title_home), getString(R.string.title_notifications), getString(R.string.title_public_local), getString(R.string.title_public_federated) };
    for (int i = 0; i < 4; i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setIcon(tabIcons[i]);
        tab.setContentDescription(pageTitles[i]);
    }
    if (tabPosition != 0) {
        TabLayout.Tab tab = tabLayout.getTabAt(tabPosition);
        if (tab != null) {
            tab.select();
        } else {
            tabPosition = 0;
        }
    }
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            tintTab(tab, true);
            if (tab.getPosition() == 1) {
                NotificationHelper.clearNotificationsForActiveAccount(MainActivity.this);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tintTab(tab, false);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    for (int i = 0; i < 4; i++) {
        tintTab(tabLayout.getTabAt(i), i == tabPosition);
    }
    // Setup push notifications
    if (NotificationHelper.areNotificationsEnabled(this)) {
        enablePushNotifications();
    } else {
        disablePushNotifications();
    }
    composeButton = floatingBtn;
}
Also used : Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) TimelinePagerAdapter(com.keylesspalace.tusky.pager.TimelinePagerAdapter) AccountEntity(com.keylesspalace.tusky.db.AccountEntity) ImageButton(android.widget.ImageButton) TabLayout(android.support.design.widget.TabLayout) FloatingActionButton(android.support.design.widget.FloatingActionButton) AccountManager(com.keylesspalace.tusky.db.AccountManager)

Example 4 with AccountManager

use of com.keylesspalace.tusky.db.AccountManager in project Tusky by tuskyapp.

the class TuskyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB").allowMainThreadQueries().addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5).build();
    accountManager = new AccountManager(db);
    serviceLocator = new ServiceLocator() {

        @Override
        public <T> T get(Class<T> clazz) {
            if (clazz.equals(AccountManager.class)) {
                // noinspection unchecked
                return (T) accountManager;
            } else {
                throw new IllegalArgumentException("Unknown service " + clazz);
            }
        }
    };
    AppInjector.INSTANCE.init(this);
    initPicasso();
    JobManager.create(this).addJobCreator(notificationPullJobCreator);
    uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
    // necessary for Android < APi 21
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Also used : AccountManager(com.keylesspalace.tusky.db.AccountManager)

Example 5 with AccountManager

use of com.keylesspalace.tusky.db.AccountManager in project Tusky by tuskyapp.

the class NotificationHelper method clearNotificationsForActiveAccount.

public static void clearNotificationsForActiveAccount(Context context) {
    AccountManager accountManager = TuskyApplication.getInstance(context).getServiceLocator().get(AccountManager.class);
    AccountEntity account = accountManager.getActiveAccount();
    if (account != null) {
        account.setActiveNotifications("[]");
        accountManager.saveAccount(account);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // noinspection ConstantConditions
        notificationManager.cancel((int) account.getId());
    }
}
Also used : NotificationManager(android.app.NotificationManager) AccountManager(com.keylesspalace.tusky.db.AccountManager) AccountEntity(com.keylesspalace.tusky.db.AccountEntity)

Aggregations

AccountManager (com.keylesspalace.tusky.db.AccountManager)5 AccountEntity (com.keylesspalace.tusky.db.AccountEntity)4 Intent (android.content.Intent)2 NotificationManager (android.app.NotificationManager)1 Drawable (android.graphics.drawable.Drawable)1 CheckBoxPreference (android.preference.CheckBoxPreference)1 EditTextPreference (android.preference.EditTextPreference)1 Preference (android.preference.Preference)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 TabLayout (android.support.design.widget.TabLayout)1 ImageButton (android.widget.ImageButton)1 PreferencesActivity (com.keylesspalace.tusky.PreferencesActivity)1 Notification (com.keylesspalace.tusky.entity.Notification)1 TimelinePagerAdapter (com.keylesspalace.tusky.pager.TimelinePagerAdapter)1 BigInteger (java.math.BigInteger)1