Search in sources :

Example 1 with NotificationJobScheduler

use of me.ccrama.redditslide.Notifications.NotificationJobScheduler in project Slide by ccrama.

the class ListViewRemoteViewsFactory method onCreate.

// Initialize the data set.
public void onCreate() {
    // In onCreate() you set up any connections / cursors to your data source. Heavy lifting,
    // for example downloading or creating content etc, should be deferred to onDataSetChanged()
    // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
    records = new ArrayList<>();
    if (NetworkUtil.isConnected(mContext)) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                if (Authentication.reddit == null) {
                    new Authentication(mContext.getApplicationContext());
                    Authentication.me = Authentication.reddit.me();
                    Authentication.mod = Authentication.me.isMod();
                    Authentication.authentication.edit().putBoolean(Reddit.SHARED_PREF_IS_MOD, Authentication.mod).apply();
                    if (Reddit.notificationTime != -1) {
                        Reddit.notifications = new NotificationJobScheduler(mContext);
                        Reddit.notifications.start(mContext.getApplicationContext());
                    }
                    if (Reddit.cachedData.contains("toCache")) {
                        Reddit.autoCache = new AutoCacheScheduler(mContext);
                        Reddit.autoCache.start(mContext.getApplicationContext());
                    }
                    final String name = Authentication.me.getFullName();
                    Authentication.name = name;
                    LogUtil.v("AUTHENTICATED");
                    if (Authentication.reddit.isAuthenticated()) {
                        final Set<String> accounts = Authentication.authentication.getStringSet("accounts", new HashSet<String>());
                        if (accounts.contains(name)) {
                            // convert to new system
                            accounts.remove(name);
                            accounts.add(name + ":" + Authentication.refresh);
                            Authentication.authentication.edit().putStringSet("accounts", accounts).apply();
                        }
                        Authentication.isLoggedIn = true;
                        Reddit.notFirst = true;
                    }
                }
                String sub = SubredditWidgetProvider.getSubFromId(id, mContext);
                Paginator p;
                if (sub.equals("frontpage")) {
                    p = new SubredditPaginator(Authentication.reddit);
                } else if (!sub.contains(".")) {
                    p = new SubredditPaginator(Authentication.reddit, sub);
                } else {
                    p = new DomainPaginator(Authentication.reddit, sub);
                }
                p.setLimit(50);
                switch(SubredditWidgetProvider.getSorting(id, mContext)) {
                    case 0:
                        p.setSorting(Sorting.HOT);
                        break;
                    case 1:
                        p.setSorting(Sorting.NEW);
                        break;
                    case 2:
                        p.setSorting(Sorting.RISING);
                        break;
                    case 3:
                        p.setSorting(Sorting.TOP);
                        break;
                    case 4:
                        p.setSorting(Sorting.CONTROVERSIAL);
                        break;
                }
                switch(SubredditWidgetProvider.getSortingTime(id, mContext)) {
                    case 0:
                        p.setTimePeriod(TimePeriod.HOUR);
                        break;
                    case 1:
                        p.setTimePeriod(TimePeriod.DAY);
                        break;
                    case 2:
                        p.setTimePeriod(TimePeriod.WEEK);
                        break;
                    case 3:
                        p.setTimePeriod(TimePeriod.MONTH);
                        break;
                    case 4:
                        p.setTimePeriod(TimePeriod.YEAR);
                        break;
                    case 5:
                        p.setTimePeriod(TimePeriod.ALL);
                        break;
                }
                try {
                    ArrayList<Submission> s = new ArrayList<>(p.next());
                    records = new ArrayList<>();
                    for (Submission subm : s) {
                        if (!PostMatch.doesMatch(subm) && !subm.isStickied()) {
                            records.add(subm);
                        }
                    }
                } catch (Exception e) {
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                Intent widgetUpdateIntent = new Intent(mContext, SubredditWidgetProvider.class);
                widgetUpdateIntent.setAction(SubredditWidgetProvider.UPDATE_MEETING_ACTION);
                widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
                mContext.sendBroadcast(widgetUpdateIntent);
            }
        }.execute();
    } else {
        Intent widgetUpdateIntent = new Intent(mContext, SubredditWidgetProvider.class);
        widgetUpdateIntent.setAction(SubredditWidgetProvider.UPDATE_MEETING_ACTION);
        widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        mContext.sendBroadcast(widgetUpdateIntent);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AutoCacheScheduler(me.ccrama.redditslide.Autocache.AutoCacheScheduler) Submission(net.dean.jraw.models.Submission) ArrayList(java.util.ArrayList) DomainPaginator(net.dean.jraw.paginators.DomainPaginator) Intent(android.content.Intent) NotificationJobScheduler(me.ccrama.redditslide.Notifications.NotificationJobScheduler) DomainPaginator(net.dean.jraw.paginators.DomainPaginator) SubredditPaginator(net.dean.jraw.paginators.SubredditPaginator) Paginator(net.dean.jraw.paginators.Paginator) SubredditPaginator(net.dean.jraw.paginators.SubredditPaginator) Authentication(me.ccrama.redditslide.Authentication) HashSet(java.util.HashSet)

Example 2 with NotificationJobScheduler

use of me.ccrama.redditslide.Notifications.NotificationJobScheduler in project Slide by ccrama.

the class SettingsGeneral method setupNotificationSettings.

public static void setupNotificationSettings(View dialoglayout, final Activity context) {
    final AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
    final Slider landscape = (Slider) dialoglayout.findViewById(R.id.landscape);
    final CheckBox checkBox = (CheckBox) dialoglayout.findViewById(R.id.load);
    final CheckBox sound = (CheckBox) dialoglayout.findViewById(R.id.sound);
    sound.setChecked(SettingValues.notifSound);
    sound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SettingValues.prefs.edit().putBoolean(SettingValues.PREF_SOUND_NOTIFS, isChecked).apply();
            SettingValues.notifSound = isChecked;
        }
    });
    if (Reddit.notificationTime == -1) {
        checkBox.setChecked(false);
        checkBox.setText(context.getString(R.string.settings_mail_check));
    } else {
        checkBox.setChecked(true);
        landscape.setValue(Reddit.notificationTime / 15, false);
        checkBox.setText(context.getString(R.string.settings_notification_newline, TimeUtils.getTimeInHoursAndMins(Reddit.notificationTime, context.getBaseContext())));
    }
    landscape.setOnPositionChangeListener(new Slider.OnPositionChangeListener() {

        @Override
        public void onPositionChanged(Slider slider, boolean b, float v, float v1, int i, int i1) {
            if (checkBox.isChecked()) {
                checkBox.setText(context.getString(R.string.settings_notification, TimeUtils.getTimeInHoursAndMins(i1 * 15, context.getBaseContext())));
            }
        }
    });
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                Reddit.notificationTime = -1;
                Reddit.colors.edit().putInt("notificationOverride", -1).apply();
                checkBox.setText(context.getString(R.string.settings_mail_check));
                landscape.setValue(0, true);
                if (Reddit.notifications != null) {
                    Reddit.notifications.cancel(context.getApplication());
                }
            } else {
                Reddit.notificationTime = 60;
                landscape.setValue(4, true);
                checkBox.setText(context.getString(R.string.settings_notification, TimeUtils.getTimeInHoursAndMins(Reddit.notificationTime, context.getBaseContext())));
            }
        }
    });
    dialoglayout.findViewById(R.id.title).setBackgroundColor(Palette.getDefaultColor());
    // todo final Slider portrait = (Slider) dialoglayout.findViewById(R.id.portrait);
    // todo  portrait.setBackgroundColor(Palette.getDefaultColor());
    final Dialog dialog = builder.setView(dialoglayout).create();
    dialog.show();
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            if (checkBox.isChecked()) {
                Reddit.notificationTime = landscape.getValue() * 15;
                Reddit.colors.edit().putInt("notificationOverride", landscape.getValue() * 15).apply();
                if (Reddit.notifications == null) {
                    Reddit.notifications = new NotificationJobScheduler(context.getApplication());
                }
                Reddit.notifications.cancel(context.getApplication());
                Reddit.notifications.start(context.getApplication());
            }
        }
    });
    dialoglayout.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View d) {
            if (checkBox.isChecked()) {
                Reddit.notificationTime = landscape.getValue() * 15;
                Reddit.colors.edit().putInt("notificationOverride", landscape.getValue() * 15).apply();
                if (Reddit.notifications == null) {
                    Reddit.notifications = new NotificationJobScheduler(context.getApplication());
                }
                Reddit.notifications.cancel(context.getApplication());
                Reddit.notifications.start(context.getApplication());
                dialog.dismiss();
                if (context instanceof SettingsGeneral) {
                    ((TextView) context.findViewById(R.id.notifications_current)).setText(context.getString(R.string.settings_notification_short, TimeUtils.getTimeInHoursAndMins(Reddit.notificationTime, context.getBaseContext())));
                }
            } else {
                Reddit.notificationTime = -1;
                Reddit.colors.edit().putInt("notificationOverride", -1).apply();
                if (Reddit.notifications == null) {
                    Reddit.notifications = new NotificationJobScheduler(context.getApplication());
                }
                Reddit.notifications.cancel(context.getApplication());
                dialog.dismiss();
                if (context instanceof SettingsGeneral) {
                    ((TextView) context.findViewById(R.id.notifications_current)).setText(R.string.settings_notifdisabled);
                }
            }
        }
    });
}
Also used : Slider(com.rey.material.widget.Slider) DialogInterface(android.content.DialogInterface) NotificationJobScheduler(me.ccrama.redditslide.Notifications.NotificationJobScheduler) View(android.view.View) TextView(android.widget.TextView) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) CheckBox(android.widget.CheckBox) Dialog(android.app.Dialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) CompoundButton(android.widget.CompoundButton)

Example 3 with NotificationJobScheduler

use of me.ccrama.redditslide.Notifications.NotificationJobScheduler in project Slide by ccrama.

the class Inbox method onCreate.

@Override
public void onCreate(Bundle savedInstance) {
    overrideSwipeFromAnywhere();
    if (Authentication.reddit == null || !Authentication.reddit.isAuthenticated() || Authentication.me == null) {
        LogUtil.v("Reauthenticating");
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                if (Authentication.reddit == null) {
                    new Authentication(getApplicationContext());
                }
                try {
                    Authentication.me = Authentication.reddit.me();
                    Authentication.mod = Authentication.me.isMod();
                    Authentication.authentication.edit().putBoolean(Reddit.SHARED_PREF_IS_MOD, Authentication.mod).apply();
                    if (Reddit.notificationTime != -1) {
                        Reddit.notifications = new NotificationJobScheduler(Inbox.this);
                        Reddit.notifications.start(getApplicationContext());
                    }
                    if (Reddit.cachedData.contains("toCache")) {
                        Reddit.autoCache = new AutoCacheScheduler(Inbox.this);
                        Reddit.autoCache.start(getApplicationContext());
                    }
                    final String name = Authentication.me.getFullName();
                    Authentication.name = name;
                    LogUtil.v("AUTHENTICATED");
                    UserSubscriptions.doCachedModSubs();
                    if (Authentication.reddit.isAuthenticated()) {
                        final Set<String> accounts = Authentication.authentication.getStringSet("accounts", new HashSet<String>());
                        if (accounts.contains(name)) {
                            // convert to new system
                            accounts.remove(name);
                            accounts.add(name + ":" + Authentication.refresh);
                            Authentication.authentication.edit().putStringSet("accounts", accounts).apply();
                        }
                        Authentication.isLoggedIn = true;
                        Reddit.notFirst = true;
                    }
                } catch (Exception ignored) {
                }
                return null;
            }
        }.execute();
    }
    super.onCreate(savedInstance);
    last = SettingValues.prefs.getLong("lastInbox", System.currentTimeMillis() - (60 * 1000 * 60));
    SettingValues.prefs.edit().putLong("lastInbox", System.currentTimeMillis()).apply();
    applyColorTheme("");
    setContentView(R.layout.activity_inbox);
    setupAppBar(R.id.toolbar, R.string.title_inbox, true, true);
    mToolbar.setPopupTheme(new ColorPreferences(this).getFontStyle().getBaseId());
    tabs = (TabLayout) findViewById(R.id.sliding_tabs);
    tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabs.setSelectedTabIndicatorColor(new ColorPreferences(Inbox.this).getColor("no sub"));
    pager = (ViewPager) findViewById(R.id.content_view);
    findViewById(R.id.header).setBackgroundColor(Palette.getDefaultColor());
    adapter = new OverviewPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(adapter);
    if (getIntent() != null && getIntent().hasExtra(EXTRA_UNREAD)) {
        pager.setCurrentItem(1);
    }
    tabs.setupWithViewPager(pager);
    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            findViewById(R.id.header).animate().translationY(0).setInterpolator(new LinearInterpolator()).setDuration(180);
            if (position == 3 && findViewById(R.id.read) != null) {
                findViewById(R.id.read).setVisibility(View.GONE);
            } else if (findViewById(R.id.read) != null) {
                findViewById(R.id.read).setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AutoCacheScheduler(me.ccrama.redditslide.Autocache.AutoCacheScheduler) ColorPreferences(me.ccrama.redditslide.ColorPreferences) NotificationJobScheduler(me.ccrama.redditslide.Notifications.NotificationJobScheduler) ViewPager(android.support.v4.view.ViewPager) LinearInterpolator(android.view.animation.LinearInterpolator) Authentication(me.ccrama.redditslide.Authentication) HashSet(java.util.HashSet)

Example 4 with NotificationJobScheduler

use of me.ccrama.redditslide.Notifications.NotificationJobScheduler in project Slide by ccrama.

the class CommentsScreenSingle method onCreate.

@Override
public void onCreate(Bundle savedInstance) {
    disableSwipeBackLayout();
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    getWindow().getDecorView().setBackgroundDrawable(null);
    super.onCreate(savedInstance);
    applyColorTheme();
    setContentView(R.layout.activity_slide);
    name = getIntent().getExtras().getString(EXTRA_SUBMISSION, "");
    subreddit = getIntent().getExtras().getString(EXTRA_SUBREDDIT, "");
    np = getIntent().getExtras().getBoolean(EXTRA_NP, false);
    context = getIntent().getExtras().getString(EXTRA_CONTEXT, "");
    contextNumber = getIntent().getExtras().getInt(EXTRA_CONTEXT_NUMBER, 5);
    if (subreddit.equals(Reddit.EMPTY_STRING)) {
        new AsyncGetSubredditName().execute(name);
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.activity_background, typedValue, true);
        int color = typedValue.data;
        findViewById(R.id.content_view).setBackgroundColor(color);
    } else {
        setupAdapter();
    }
    if (Authentication.isLoggedIn && Authentication.me == null) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                if (Authentication.reddit == null) {
                    new Authentication(getApplicationContext());
                } else {
                    try {
                        Authentication.me = Authentication.reddit.me();
                        Authentication.mod = Authentication.me.isMod();
                        Authentication.authentication.edit().putBoolean(Reddit.SHARED_PREF_IS_MOD, Authentication.mod).apply();
                        if (Reddit.notificationTime != -1) {
                            Reddit.notifications = new NotificationJobScheduler(CommentsScreenSingle.this);
                            Reddit.notifications.start(getApplicationContext());
                        }
                        if (Reddit.cachedData.contains("toCache")) {
                            Reddit.autoCache = new AutoCacheScheduler(CommentsScreenSingle.this);
                            Reddit.autoCache.start(getApplicationContext());
                        }
                        final String name = Authentication.me.getFullName();
                        Authentication.name = name;
                        LogUtil.v("AUTHENTICATED");
                        UserSubscriptions.doCachedModSubs();
                        if (Authentication.reddit.isAuthenticated()) {
                            final Set<String> accounts = Authentication.authentication.getStringSet("accounts", new HashSet<String>());
                            if (accounts.contains(name)) {
                                // convert to new system
                                accounts.remove(name);
                                accounts.add(name + ":" + Authentication.refresh);
                                Authentication.authentication.edit().putStringSet("accounts", accounts).apply();
                            }
                            Authentication.isLoggedIn = true;
                            Reddit.notFirst = true;
                        }
                    } catch (Exception e) {
                        new Authentication(getApplicationContext());
                    }
                }
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AutoCacheScheduler(me.ccrama.redditslide.Autocache.AutoCacheScheduler) NotificationJobScheduler(me.ccrama.redditslide.Notifications.NotificationJobScheduler) ColorDrawable(android.graphics.drawable.ColorDrawable) Authentication(me.ccrama.redditslide.Authentication) TypedValue(android.util.TypedValue) HashSet(java.util.HashSet)

Aggregations

NotificationJobScheduler (me.ccrama.redditslide.Notifications.NotificationJobScheduler)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Authentication (me.ccrama.redditslide.Authentication)3 AutoCacheScheduler (me.ccrama.redditslide.Autocache.AutoCacheScheduler)3 Dialog (android.app.Dialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 ViewPager (android.support.v4.view.ViewPager)1 TypedValue (android.util.TypedValue)1 View (android.view.View)1 LinearInterpolator (android.view.animation.LinearInterpolator)1 CheckBox (android.widget.CheckBox)1 CompoundButton (android.widget.CompoundButton)1 TextView (android.widget.TextView)1 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 Slider (com.rey.material.widget.Slider)1 ArrayList (java.util.ArrayList)1