Android Open Source - Rss-App-for-tumblr Sparse R S S App Widget Provider






From Project

Back to project page Rss-App-for-tumblr.

License

The source code is released under:

GNU General Public License

If you think the Android project Rss-App-for-tumblr 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

/**
 * Ace Shooting/*from  ww w.j a  v a 2s .  c  om*/
 *
 * Copyright (c) 2010-2012 Stefan Handschuh
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

package com.aceshooting.rssapp.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.RemoteViews;

import com.aceshooting.rssapp.MainTabActivity;
import com.aceshooting.rssapp.R;
import com.aceshooting.rssapp.Strings;
import com.aceshooting.rssapp.provider.FeedData;

public class SparseRSSAppWidgetProvider extends AppWidgetProvider {
  private static final String LIMIT = " limit ";
  
  private static final int[] IDS = {R.id.news_1, R.id.news_2, R.id.news_3, R.id.news_4, R.id.news_5, R.id.news_6, R.id.news_7, R.id.news_8, R.id.news_9, R.id.news_10};
  
  private static final int[] ICON_IDS = {R.id.news_icon_1, R.id.news_icon_2, R.id.news_icon_3, R.id.news_icon_4, R.id.news_icon_5, R.id.news_icon_6, R.id.news_icon_7, R.id.news_icon_8, R.id.news_icon_9, R.id.news_icon_10};
  
  public static final int STANDARD_BACKGROUND = 0x7c000000;
  
  @Override
  public void onReceive(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    
    onUpdate(context, appWidgetManager, appWidgetManager.getAppWidgetIds(new ComponentName(context, SparseRSSAppWidgetProvider.class)));
  }
  
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    SharedPreferences preferences = context.getSharedPreferences(SparseRSSAppWidgetProvider.class.getName(), 0);
    
    for (int n = 0, i = appWidgetIds.length; n < i; n++) {
      updateAppWidget(context, appWidgetManager, appWidgetIds[n], preferences.getBoolean(appWidgetIds[n]+".hideread", false), preferences.getString(appWidgetIds[n]+".entrycount", "10"), preferences.getString(appWidgetIds[n]+".feeds", Strings.EMPTY), preferences.getInt(appWidgetIds[n]+".background", STANDARD_BACKGROUND));
    }
  }
  
  static void updateAppWidget(Context context, int appWidgetId, boolean hideRead, String entryCount, String feedIds, int backgroundColor) {
    updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId, hideRead, entryCount, feedIds, backgroundColor);
  }
  
  private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, boolean hideRead, String entryCount, String feedIds, int backgroundColor) {
    StringBuilder selection = new StringBuilder();
    
    if (hideRead) {
      selection.append(FeedData.EntryColumns.READDATE).append(Strings.DB_ISNULL);
    }
    
    if (feedIds.length() > 0) {
      if (selection.length() > 0) {
        selection.append(Strings.DB_AND);
      }
      selection.append(FeedData.EntryColumns.FEED_ID).append(" IN ("+feedIds).append(')');
    }
    
    Cursor cursor = context.getContentResolver().query(FeedData.EntryColumns.CONTENT_URI, new String[] {FeedData.EntryColumns.TITLE, FeedData.EntryColumns._ID, FeedData.FeedColumns.ICON}, selection.toString(), null, new StringBuilder(FeedData.EntryColumns.DATE).append(Strings.DB_DESC).append(LIMIT).append(entryCount).toString());
    
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.homescreenwidget);
    
    views.setOnClickPendingIntent(R.id.feed_icon, PendingIntent.getActivity(context, 0, new Intent(context, MainTabActivity.class), 0));
    
    int k = 0;
    
    while (cursor.moveToNext() && k < IDS.length) {
      views.setViewVisibility(IDS[k], View.VISIBLE);
      if (!cursor.isNull(2)) {
        try {
          byte[] iconBytes = cursor.getBlob(2);
          
          if (iconBytes != null && iconBytes.length > 0) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
            
            if (bitmap != null) {
              views.setBitmap(ICON_IDS[k], "setImageBitmap", bitmap);
              views.setViewVisibility(ICON_IDS[k], View.VISIBLE);
              views.setTextViewText(IDS[k], " "+cursor.getString(0)); // bad style
            } else {
              views.setViewVisibility(ICON_IDS[k], View.GONE);
              views.setTextViewText(IDS[k], cursor.getString(0));
            }
          } else {
            views.setViewVisibility(ICON_IDS[k], View.GONE);
            views.setTextViewText(IDS[k], cursor.getString(0));
          }
        } catch (Throwable e) {
          views.setViewVisibility(ICON_IDS[k], View.GONE);
          views.setTextViewText(IDS[k], cursor.getString(0));
        }
      } else {
        views.setViewVisibility(ICON_IDS[k], View.GONE);
        views.setTextViewText(IDS[k], cursor.getString(0));
      }
      views.setOnClickPendingIntent(IDS[k++], PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, FeedData.EntryColumns.ENTRY_CONTENT_URI(cursor.getString(1))), PendingIntent.FLAG_CANCEL_CURRENT));
    }
    cursor.close();
    for (; k < IDS.length; k++) {
      views.setViewVisibility(ICON_IDS[k], View.GONE);
      views.setViewVisibility(IDS[k], View.GONE);
      views.setTextViewText(IDS[k], Strings.EMPTY);
    }
    views.setInt(R.id.widgetlayout, "setBackgroundColor", backgroundColor);
    appWidgetManager.updateAppWidget(appWidgetId, views);
  }

}




Java Source Code List

com.aceshooting.rssapp.Animations.java
com.aceshooting.rssapp.ApplicationPreferencesActivity.java
com.aceshooting.rssapp.BASE64.java
com.aceshooting.rssapp.BootCompletedBroadcastReceiver.java
com.aceshooting.rssapp.CompatibilityHelper.java
com.aceshooting.rssapp.EmptyActivity.java
com.aceshooting.rssapp.EntriesListActivity.java
com.aceshooting.rssapp.EntriesListAdapter.java
com.aceshooting.rssapp.EntryActivity.java
com.aceshooting.rssapp.FeedConfigActivity.java
com.aceshooting.rssapp.MainTabActivity.java
com.aceshooting.rssapp.MyApplication.java
com.aceshooting.rssapp.RSSOverviewListAdapter.java
com.aceshooting.rssapp.RSSOverview.java
com.aceshooting.rssapp.RefreshBroadcastReceiver.java
com.aceshooting.rssapp.Requeryable.java
com.aceshooting.rssapp.SimpleTask.java
com.aceshooting.rssapp.Strings.java
com.aceshooting.rssapp.handler.PictureFilenameFilter.java
com.aceshooting.rssapp.handler.RSSHandler.java
com.aceshooting.rssapp.provider.FeedDataContentProvider.java
com.aceshooting.rssapp.provider.FeedData.java
com.aceshooting.rssapp.provider.OPML.java
com.aceshooting.rssapp.service.FetcherService.java
com.aceshooting.rssapp.service.RefreshService.java
com.aceshooting.rssapp.widget.ColorPickerDialogPreference.java
com.aceshooting.rssapp.widget.SparseRSSAppWidgetProvider.java
com.aceshooting.rssapp.widget.WidgetConfigActivity.java