Example usage for android.database Cursor moveToPosition

List of usage examples for android.database Cursor moveToPosition

Introduction

In this page you can find the example usage for android.database Cursor moveToPosition.

Prototype

boolean moveToPosition(int position);

Source Link

Document

Move the cursor to an absolute position.

Usage

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void editDontWantToSee() {
    if (!SettingConstants.UPDATING_FILTER) {
        Set<String> currentExclusions = PrefUtils.getStringSetValue(R.string.I_DONT_WANT_TO_SEE_ENTRIES, null);

        final ArrayList<ExclusionEdit> mCurrentExclusionList = new ArrayList<TvBrowser.ExclusionEdit>();

        if (currentExclusions != null && !currentExclusions.isEmpty()) {
            for (String exclusion : currentExclusions) {
                mCurrentExclusionList.add(new ExclusionEdit(exclusion));
            }//from  w w w.  ja va2 s  .c om
        }

        Collections.sort(mCurrentExclusionList);

        final ArrayAdapter<ExclusionEdit> exclusionAdapter = new ArrayAdapter<TvBrowser.ExclusionEdit>(
                TvBrowser.this, android.R.layout.simple_list_item_1, mCurrentExclusionList);

        View view = getLayoutInflater().inflate(R.layout.dont_want_to_see_exclusion_edit_list,
                getParentViewGroup(), false);

        ListView list = (ListView) view.findViewById(R.id.dont_want_to_see_exclusion_list);

        list.setAdapter(exclusionAdapter);

        final Runnable cancel = new Runnable() {
            @Override
            public void run() {
            }
        };

        AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final ExclusionEdit edit = exclusionAdapter.getItem(position);

                View editView = getLayoutInflater().inflate(R.layout.dont_want_to_see_edit,
                        getParentViewGroup(), false);

                final TextView exclusion = (TextView) editView.findViewById(R.id.dont_want_to_see_value);
                final CheckBox caseSensitive = (CheckBox) editView
                        .findViewById(R.id.dont_want_to_see_case_sensitve);

                exclusion.setText(edit.mExclusion);
                caseSensitive.setSelected(edit.mIsCaseSensitive);

                Runnable editPositive = new Runnable() {
                    @Override
                    public void run() {
                        if (exclusion.getText().toString().trim().length() > 0) {
                            edit.mExclusion = exclusion.getText().toString();
                            edit.mIsCaseSensitive = caseSensitive.isSelected();

                            exclusionAdapter.notifyDataSetChanged();
                        }
                    }
                };

                showAlertDialog(getString(R.string.action_dont_want_to_see), null, editView, null, editPositive,
                        null, cancel, false, false);
            }
        };

        list.setOnItemClickListener(onClickListener);
        list.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                getMenuInflater().inflate(R.menu.don_want_to_see_context, menu);

                MenuItem item = menu.findItem(R.id.dont_want_to_see_delete);

                item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        ExclusionEdit edit = exclusionAdapter
                                .getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position);
                        exclusionAdapter.remove(edit);
                        exclusionAdapter.notifyDataSetChanged();

                        return true;
                    }
                });
            }
        });

        Thread positive = new Thread() {
            @Override
            public void run() {
                SettingConstants.UPDATING_FILTER = true;

                final NotificationCompat.Builder builder = new NotificationCompat.Builder(TvBrowser.this);
                builder.setSmallIcon(R.drawable.ic_stat_notify);
                builder.setOngoing(true);
                builder.setContentTitle(getResources().getText(R.string.action_dont_want_to_see));
                builder.setContentText(
                        getResources().getText(R.string.dont_want_to_see_refresh_notification_text));

                final int notifyID = 2;

                final NotificationManager notification = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                notification.notify(notifyID, builder.build());

                updateProgressIcon(true);

                HashSet<String> newExclusions = new HashSet<String>();
                final ArrayList<DontWantToSeeExclusion> exclusionList = new ArrayList<DontWantToSeeExclusion>();

                for (ExclusionEdit edit : mCurrentExclusionList) {
                    String exclusion = edit.getExclusion();

                    newExclusions.add(exclusion);
                    exclusionList.add(new DontWantToSeeExclusion(exclusion));
                }

                new Thread() {
                    public void run() {
                        Cursor programs = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_DATA,
                                new String[] { TvBrowserContentProvider.KEY_ID,
                                        TvBrowserContentProvider.DATA_KEY_TITLE },
                                null, null, TvBrowserContentProvider.KEY_ID);
                        programs.moveToPosition(-1);

                        builder.setProgress(programs.getCount(), 0, true);
                        notification.notify(notifyID, builder.build());

                        ArrayList<ContentProviderOperation> updateValuesList = new ArrayList<ContentProviderOperation>();

                        int keyColumn = programs.getColumnIndex(TvBrowserContentProvider.KEY_ID);
                        int titleColumn = programs.getColumnIndex(TvBrowserContentProvider.DATA_KEY_TITLE);

                        DontWantToSeeExclusion[] exclusionArr = exclusionList
                                .toArray(new DontWantToSeeExclusion[exclusionList.size()]);

                        while (programs.moveToNext()) {
                            builder.setProgress(programs.getCount(), programs.getPosition(), false);
                            notification.notify(notifyID, builder.build());

                            String title = programs.getString(titleColumn);

                            boolean filter = UiUtils.filter(getApplicationContext(), title, exclusionArr);
                            long progID = programs.getLong(keyColumn);

                            ContentValues values = new ContentValues();
                            values.put(TvBrowserContentProvider.DATA_KEY_DONT_WANT_TO_SEE, filter ? 1 : 0);

                            ContentProviderOperation.Builder opBuilder = ContentProviderOperation.newUpdate(
                                    ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_DATA_UPDATE,
                                            progID));
                            opBuilder.withValues(values);

                            updateValuesList.add(opBuilder.build());
                        }

                        notification.cancel(notifyID);

                        programs.close();

                        if (!updateValuesList.isEmpty()) {
                            try {
                                getContentResolver().applyBatch(TvBrowserContentProvider.AUTHORITY,
                                        updateValuesList);
                                UiUtils.sendDontWantToSeeChangedBroadcast(getApplicationContext(), true);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(),
                                                R.string.dont_want_to_see_sync_success, Toast.LENGTH_LONG)
                                                .show();
                                    }
                                });
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            } catch (OperationApplicationException e) {
                                e.printStackTrace();
                            }
                        }

                        updateProgressIcon(false);
                        SettingConstants.UPDATING_FILTER = false;
                    }
                }.start();

                Editor edit = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
                edit.putStringSet(getString(R.string.I_DONT_WANT_TO_SEE_ENTRIES), newExclusions);
                edit.commit();
            }
        };

        showAlertDialog(getString(R.string.action_dont_want_to_see_edit), null, view, null, positive, null,
                cancel, false, true);
    }
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    handler = new Handler();
    PrefUtils.initialize(TvBrowser.this);

    Intent start = getIntent();//www .j a  va 2 s. c o  m

    if (start != null && start.hasExtra(SettingConstants.CHANNEL_ID_EXTRA)) {
        mProgramListChannelId = start.getIntExtra(SettingConstants.CHANNEL_ID_EXTRA,
                FragmentProgramsList.NO_CHANNEL_SELECTION_ID);
        mProgramListScrollTime = start.getLongExtra(SettingConstants.START_TIME_EXTRA, -1);
    }

    /*
     * Hack to force overflow menu button to be shown from:
     * http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button
     */
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

    try {
        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

        int oldVersion = PrefUtils.getIntValueWithDefaultKey(R.string.OLD_VERSION,
                R.integer.old_version_default);
        //FAVORITE_LIST
        if (oldVersion > getResources().getInteger(R.integer.old_version_default) && oldVersion < 309) {
            new Thread("READ REMINDERS ONCE FOR ICON") {
                @Override
                public void run() {
                    Cursor reminders = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_DATA,
                            new String[] { TvBrowserContentProvider.KEY_ID },
                            TvBrowserContentProvider.DATA_KEY_MARKING_REMINDER + " OR "
                                    + TvBrowserContentProvider.DATA_KEY_MARKING_FAVORITE_REMINDER,
                            null, TvBrowserContentProvider.KEY_ID);

                    try {
                        reminders.moveToPosition(-1);

                        int idColumn = reminders.getColumnIndex(TvBrowserContentProvider.KEY_ID);
                        ArrayList<String> reminderIdList = new ArrayList<String>();

                        while (reminders.moveToNext()) {
                            reminderIdList.add(String.valueOf(reminders.getLong(idColumn)));
                        }

                        ProgramUtils.addReminderIds(getApplicationContext(), reminderIdList);
                    } finally {
                        IOUtils.closeCursor(reminders);
                    }
                };
            }.start();
        }
        if (oldVersion < 304) {
            Set<String> favoritesSet = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this)
                    .getStringSet("FAVORITE_LIST", new HashSet<String>());

            int id = 1000;

            for (String favorite : favoritesSet) {
                Favorite fav = new Favorite(id++, favorite);

                if (fav.isValid()) {
                    fav.save(getApplicationContext());
                } else {
                    Favorite.handleFavoriteMarking(TvBrowser.this, fav, Favorite.TYPE_MARK_REMOVE);
                }
            }

            Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
            edit.remove("FAVORITE_LIST");
            edit.commit();
        }
        if (oldVersion < 204) {
            int firstTime = PrefUtils.getStringValueAsInt(R.string.PREF_REMINDER_TIME,
                    R.string.pref_reminder_time_default);
            boolean remindAgain = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this)
                    .getBoolean("PREF_REMIND_AGAIN_AT_START", false);
            Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
            edit.remove("PREF_REMIND_AGAIN_AT_START");
            edit.commit();

            if (remindAgain && firstTime > 0) {
                edit.putString(getString(R.string.PREF_REMINDER_TIME_SECOND),
                        getString(R.string.pref_reminder_time_default));
                edit.commit();

                Intent updateAlarmValues = new Intent(UpdateAlarmValue.class.getCanonicalName());
                sendBroadcast(updateAlarmValues);
            }
        }
        if (oldVersion < 218) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this);
            Editor edit = pref.edit();

            boolean userDefined = addUserColor(pref, edit,
                    R.color.pref_color_on_air_background_tvb_style_default,
                    R.string.PREF_COLOR_ON_AIR_BACKGROUND, R.string.PREF_COLOR_ON_AIR_BACKGROUND_USER_DEFINED);
            userDefined = addUserColor(pref, edit, R.color.pref_color_on_air_progress_tvb_style_default,
                    R.string.PREF_COLOR_ON_AIR_PROGRESS, R.string.PREF_COLOR_ON_AIR_PROGRESS_USER_DEFINED)
                    || userDefined;
            userDefined = addUserColor(pref, edit, R.color.pref_color_mark_tvb_style_default,
                    R.string.PREF_COLOR_MARKED, R.string.PREF_COLOR_MARKED_USER_DEFINED) || userDefined;
            userDefined = addUserColor(pref, edit, R.color.pref_color_mark_favorite_tvb_style_default,
                    R.string.PREF_COLOR_FAVORITE, R.string.PREF_COLOR_FAVORITE) || userDefined;
            userDefined = addUserColor(pref, edit, R.color.pref_color_mark_reminder_tvb_style_default,
                    R.string.PREF_COLOR_REMINDER, R.string.PREF_COLOR_REMINDER_USER_DEFINED) || userDefined;
            userDefined = addUserColor(pref, edit, R.color.pref_color_mark_sync_tvb_style_favorite_default,
                    R.string.PREF_COLOR_SYNC, R.string.PREF_COLOR_SYNC_USER_DEFINED) || userDefined;

            if (userDefined) {
                edit.putString(getString(R.string.PREF_COLOR_STYLE), "0");
            }

            edit.commit();
        }
        if (oldVersion < 242) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this);
            Editor edit = pref.edit();

            if (pref.contains("PREF_WIDGET_BACKGROUND_TRANSPARENCY")
                    && !pref.getBoolean("PREF_WIDGET_BACKGROUND_TRANSPARENCY", true)) {
                edit.remove("PREF_WIDGET_BACKGROUND_TRANSPARENCY");
                edit.putString(getString(R.string.PREF_WIDGET_BACKGROUND_TRANSPARENCY_HEADER), "0");
                edit.putString(getString(R.string.PREF_WIDGET_BACKGROUND_TRANSPARENCY_LIST), "0");
                edit.putBoolean(getString(R.string.PREF_WIDGET_BACKGROUND_ROUNDED_CORNERS), false);
            }

            if (pref.contains("SELECTED_TV_CHANNELS_LIST")) {
                edit.remove("SELECTED_TV_CHANNELS_LIST");
            }
            if (pref.contains("SELECTED_RADIO_CHANNELS_LIST")) {
                edit.remove("SELECTED_RADIO_CHANNELS_LIST");
            }
            if (pref.contains("SELECTED_CINEMA_CHANNELS_LIST")) {
                edit.remove("SELECTED_CINEMA_CHANNELS_LIST");
            }

            edit.commit();
        }
        if (oldVersion < 284 && PrefUtils
                .getStringValue(R.string.PREF_PROGRAM_LISTS_DIVIDER_SIZE,
                        R.string.pref_program_lists_divider_size_default)
                .equals(getString(R.string.divider_small))) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this);

            Editor edit = pref.edit();
            edit.remove(getString(R.string.PREF_PROGRAM_LISTS_DIVIDER_SIZE));
            edit.commit();
        }
        if (oldVersion < 287
                && PrefUtils.getBooleanValue(R.string.PREF_WIDGET_BACKGROUND_ROUNDED_CORNERS, true)) {
            Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
            edit.remove(getString(R.string.PREF_WIDGET_BACKGROUND_ROUNDED_CORNERS));
            edit.commit();

            UiUtils.updateImportantProgramsWidget(getApplicationContext());
            UiUtils.updateRunningProgramsWidget(getApplicationContext());
        }

        if (oldVersion > getResources().getInteger(R.integer.old_version_default)
                && oldVersion < pInfo.versionCode) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this);

                    builder.setTitle(R.string.info_version);
                    builder.setMessage(Html.fromHtml(getString(R.string.info_version_new)));
                    builder.setPositiveButton(android.R.string.ok, null);

                    builder.show();
                }
            }, 5000);

        } else if (oldVersion != getResources().getInteger(R.integer.old_version_default)) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showNews();
                }
            }, 5000);
        }

        if (oldVersion != pInfo.versionCode) {
            Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();
            edit.putInt(getString(R.string.OLD_VERSION), pInfo.versionCode);
            edit.commit();
        }
    } catch (NameNotFoundException e) {
    }

    super.onCreate(savedInstanceState);

    SettingConstants.initializeLogoMap(TvBrowser.this, false);

    setContentView(R.layout.activity_tv_browser);

    mProgamListStateStack = new Stack<ProgramsListState>();

    ALL_VALUE = getResources().getString(R.string.filter_channel_all);

    if (savedInstanceState != null) {
        updateRunning = savedInstanceState.getBoolean(SettingConstants.UPDATE_RUNNING_KEY, false);
        selectingChannels = savedInstanceState.getBoolean(SettingConstants.SELECTION_CHANNELS_KEY, false);
    }

    // Set up the action bar.
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);

            Fragment fragment = mSectionsPagerAdapter.getRegisteredFragment(position);

            if (fragment instanceof ProgramTableFragment) {
                ((ProgramTableFragment) fragment).firstLoad(getLayoutInflater());
                ((ProgramTableFragment) fragment).scrollToTime(0, mScrollTimeItem);
            }

            if (mFilterItem != null) {
                mFilterItem.setVisible(!(fragment instanceof FragmentFavorites) && !mSearchExpanded);
            }
            if (mCreateFavorite != null) {
                mCreateFavorite.setVisible(fragment instanceof FragmentFavorites && !mSearchExpanded);
            }

            mProgramsListWasShow = false;

            if (position != 1) {
                mProgamListStateStack.clear();
            }
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar
                .addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }

    int startTab = Integer.parseInt(
            PrefUtils.getStringValue(R.string.TAB_TO_SHOW_AT_START, R.string.tab_to_show_at_start_default));

    if (mSectionsPagerAdapter.getCount() > startTab) {
        mViewPager.setCurrentItem(startTab);
    }

    PluginHandler.loadPlugins(getApplicationContext(), handler);

    IOUtils.handleDataUpdatePreferences(TvBrowser.this);
}