Android Open Source - TorrentFreak-Reader Stack Widget Activity






From Project

Back to project page TorrentFreak-Reader.

License

The source code is released under:

GNU General Public License

If you think the Android project TorrentFreak-Reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 Jack Wakefield//  w ww.ja  va 2  s  . co m
 *
 * This file is part of TorrentFreak Reader.
 *
 * TorrentFreak Reader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TorrentFreak Reader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TorrentFreak Reader.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.torrentfreak.reader.free.widgets;

import java.lang.CharSequence;
import java.util.List;
import android.appwidget.AppWidgetManager;
import android.content.SharedPreferences;
import android.content.Intent;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockPreferenceActivity;
import com.google.inject.Inject;
import com.torrentfreak.reader.free.categories.CategoryItem;
import com.torrentfreak.reader.free.categories.CategoryManager;
import com.torrentfreak.reader.free.R;
import com.torrentfreak.reader.free.widgets.StackWidgetProvider;

public class StackWidgetActivity extends RoboSherlockPreferenceActivity {
    /**
     * The settings key used to determine which category to retrieve articles from.
     */
    public static final String SETTING_CATEGORY = "category";

    /**
     * The settings key used to determine the number of milliseconds between retrieving the article
     * list.
     */
    public static final String SETTING_UPDATE_INTERVAL = "update_interval";

    /**
     * The category manager.
     */
    @Inject
    private CategoryManager categoryManager;

    /**
     * The ID of the widget.
     */
    private int widgetId;

    /**
     * The button used to create the widget.
     */
    private Button createButton;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // manually set the window background to the background pattern as no layout is set
        getWindow().setBackgroundDrawableResource(R.drawable.background_pattern);

        widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

        final Intent intent = getIntent();
        final Bundle extras = intent.getExtras();

        if (extras != null) {
            // retrieve the widget ID from the intent
            widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // if no widget ID has been specified or an invalid widget ID has been given, close the
        // activity
        if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        // setup the action bar
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(R.string.stack_widget_activity_title);

        // add the preference resource to display the widget settings
        addPreferencesFromResource(R.xml.stack_widget_preferences);

        final List<CategoryItem> categories = categoryManager.getCategories();
        final CharSequence[] categoryEntries = new CharSequence[categories.size()];
        final CharSequence[] categoryEntryValues = new CharSequence[categories.size()];

        // create a list of category IDs and names used to select a category for the widget
        for (int i = 0; i < categories.size(); i++) {
            final CategoryItem category = categories.get(i);
            categoryEntries[i] = category.getName();
            categoryEntryValues[i] = String.valueOf(category.getId());
        }

        // apply the category IDs and names to the category preference
        final ListPreference categoryPreference = (ListPreference)findPreference(SETTING_CATEGORY);
        categoryPreference.setEntries(categoryEntries);
        categoryPreference.setEntryValues(categoryEntryValues);

        final CategoryItem primaryCategory = categoryManager.getPrimaryCategory();

        // if one exists, set the current selected category to the primary category
        if (primaryCategory != null) {
            categoryPreference.setValue(String.valueOf(primaryCategory.getId()));
        }

        // create the create widget button and apply the click listener
        createButton = new Button(this);
        createButton.setText(R.string.create_widget);
        createButton.setOnClickListener(createWidget);

        // add create button to the footer of the list view
        final ListView listView = getListView();
        listView.addFooterView(createButton);
    }

    private final View.OnClickListener createWidget = new View.OnClickListener() {
         public void onClick(final View view) {
            final SharedPreferences sharedPreference = getBaseContext().getSharedPreferences(
                StackWidgetProvider.getSharedPreferencesNameForWidget(widgetId), 0);
            final SharedPreferences.Editor sharedPreferenceEditor = sharedPreference.edit();

            // set the category setting value to the ID of the selected category
            final ListPreference categoryPreference =
                (ListPreference)findPreference(SETTING_CATEGORY);
            sharedPreferenceEditor.putInt(SETTING_CATEGORY,
                Integer.valueOf(categoryPreference.getValue()));

            // set the update interval setting value to the millisecond value of the selected
            // interval
            final ListPreference updateIntervalPreference =
                (ListPreference)findPreference(SETTING_UPDATE_INTERVAL);
            sharedPreferenceEditor.putLong(SETTING_UPDATE_INTERVAL,
                Long.valueOf(updateIntervalPreference.getValue()));

            // save the settings
            sharedPreferenceEditor.commit();

            // create the result intent to inform the callee the widget has been created
            final Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            setResult(RESULT_OK, resultValue);

            finish();
         }
     };
}




Java Source Code List

com.slidingmenu.lib.CanvasTransformerBuilder.java
com.slidingmenu.lib.CustomViewAbove.java
com.slidingmenu.lib.CustomViewBehind.java
com.slidingmenu.lib.MenuInterface.java
com.slidingmenu.lib.SlidingMenu.java
com.slidingmenu.lib.app.SlidingActivityBase.java
com.slidingmenu.lib.app.SlidingActivityHelper.java
com.slidingmenu.lib.app.SlidingActivity.java
com.slidingmenu.lib.app.SlidingFragmentActivity.java
com.slidingmenu.lib.app.SlidingListActivity.java
com.slidingmenu.lib.app.SlidingPreferenceActivity.java
com.torrentfreak.reader.free.AboutActivity.java
com.torrentfreak.reader.free.ArticleActivity.java
com.torrentfreak.reader.free.LicensesActivity.java
com.torrentfreak.reader.free.MainActivity.java
com.torrentfreak.reader.free.SettingsActivity.java
com.torrentfreak.reader.free.adapters.ArticleFragmentAdapter.java
com.torrentfreak.reader.free.adapters.ArticleListAdapter.java
com.torrentfreak.reader.free.adapters.LicenseFragmentAdapter.java
com.torrentfreak.reader.free.adapters.SlidingMenuAdapter.java
com.torrentfreak.reader.free.adapters.items.LicenseItem.java
com.torrentfreak.reader.free.adapters.items.SlidingMenuItem.java
com.torrentfreak.reader.free.adapters.views.ArticleItemView.java
com.torrentfreak.reader.free.adapters.views.SlidingMenuItemView.java
com.torrentfreak.reader.free.articles.ArticleItem.java
com.torrentfreak.reader.free.articles.ArticleStorage.java
com.torrentfreak.reader.free.articles.providers.ArticleContentProvider.java
com.torrentfreak.reader.free.articles.providers.ArticleListProvider.java
com.torrentfreak.reader.free.articles.providers.CategoryListProvider.java
com.torrentfreak.reader.free.articles.providers.LatestNewsListProvider.java
com.torrentfreak.reader.free.articles.providers.exceptions.ArticleScrapeException.java
com.torrentfreak.reader.free.articles.tasks.ArticleContentHttpTask.java
com.torrentfreak.reader.free.categories.CategoryItem.java
com.torrentfreak.reader.free.categories.CategoryManager.java
com.torrentfreak.reader.free.categories.CategoryType.java
com.torrentfreak.reader.free.fragments.ArticleCommentsFragment.java
com.torrentfreak.reader.free.fragments.ArticleContentFragment.java
com.torrentfreak.reader.free.fragments.ArticleListFragment.java
com.torrentfreak.reader.free.fragments.LicenseFragment.java
com.torrentfreak.reader.free.fragments.SlidingMenuFragment.java
com.torrentfreak.reader.free.helpers.FontHelper.java
com.torrentfreak.reader.free.helpers.WeakReferenceHelper.java
com.torrentfreak.reader.free.widgets.StackWidgetActivity.java
com.torrentfreak.reader.free.widgets.StackWidgetProvider.java
com.torrentfreak.reader.free.widgets.StackWidgetService.java
com.torrentfreak.reader.free.widgets.items.StackWidgetItem.java