Android Open Source - PhoneProfiles_Eclipse Profile List Widget Provider






From Project

Back to project page PhoneProfiles_Eclipse.

License

The source code is released under:

Apache License

If you think the Android project PhoneProfiles_Eclipse 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

package sk.henrichg.phoneprofiles;
//w  w w . j av  a 2s .c  o m
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.widget.RemoteViews;

@SuppressLint("NewApi")
public class ProfileListWidgetProvider extends AppWidgetProvider {

  private DataWrapper dataWrapper;
  
  public static final String INTENT_REFRESH_LISTWIDGET = "sk.henrichg.phoneprofiles.REFRESH_LISTWIDGET";
  
  private boolean isLargeLayout;
  private boolean isKeyguard;
  
  @SuppressWarnings("deprecation")
    private RemoteViews buildLayout(Context context, AppWidgetManager appWidgetManager, int appWidgetId, boolean largeLayout)
  {
    Intent svcIntent=new Intent(context, ProfileListWidgetService.class);
          
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
          
    RemoteViews widget;
    
    if (largeLayout)
    {
      if (GlobalData.applicationWidgetListHeader)
      {
        if (!GlobalData.applicationWidgetListGridLayout)
        {
          if (GlobalData.applicationWidgetListPrefIndicator)
            widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget);
          else
            widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_no_indicator);
        }
        else
        {
          if (GlobalData.applicationWidgetListPrefIndicator)
            widget=new RemoteViews(context.getPackageName(), R.layout.profile_grid_widget);
          else
            widget=new RemoteViews(context.getPackageName(), R.layout.profile_grid_widget_no_indicator);
        }
      }
      else
      {
        if (!GlobalData.applicationWidgetListGridLayout)
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_no_header);
        else
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_grid_widget_no_header);
      }
    }
    else
    {
      if (isKeyguard)
      {
        if (GlobalData.applicationWidgetListPrefIndicator)
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_small_keyguard);
        else
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_small_no_indicator_keyguard);
      }
      else
      {
        if (GlobalData.applicationWidgetListPrefIndicator)
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_small);
        else
          widget=new RemoteViews(context.getPackageName(), R.layout.profile_list_widget_small_no_indicator);
      }
    }

    // set background
    int red = 0;
    int green = 0;
    int blue = 0;
    if (GlobalData.applicationWidgetListLightnessB.equals("0")) red = 0x00;
    if (GlobalData.applicationWidgetListLightnessB.equals("25")) red = 0x40;
    if (GlobalData.applicationWidgetListLightnessB.equals("50")) red = 0x80;
    if (GlobalData.applicationWidgetListLightnessB.equals("75")) red = 0xC0;
    if (GlobalData.applicationWidgetListLightnessB.equals("100")) red = 0xFF;
    green = red; blue = red;
    int alpha = 0x40;
    if (GlobalData.applicationWidgetListBackground.equals("0")) alpha = 0x00;
    if (GlobalData.applicationWidgetListBackground.equals("25")) alpha = 0x40;
    if (GlobalData.applicationWidgetListBackground.equals("50")) alpha = 0x80;
    if (GlobalData.applicationWidgetListBackground.equals("75")) alpha = 0xC0;
    if (GlobalData.applicationWidgetListBackground.equals("100")) alpha = 0xFF;
    widget.setInt(R.id.widget_profile_list_root, "setBackgroundColor", Color.argb(alpha, red, green, blue));
    
    // header
    if (GlobalData.applicationWidgetListHeader || (!largeLayout))
    {
      int monochromeValue = 0xFF;
      if (GlobalData.applicationWidgetListIconLightness.equals("0")) monochromeValue = 0x00;
      if (GlobalData.applicationWidgetListIconLightness.equals("25")) monochromeValue = 0x40;
      if (GlobalData.applicationWidgetListIconLightness.equals("50")) monochromeValue = 0x80;
      if (GlobalData.applicationWidgetListIconLightness.equals("75")) monochromeValue = 0xC0;
      if (GlobalData.applicationWidgetListIconLightness.equals("100")) monochromeValue = 0xFF;
      
      Profile profile = dataWrapper.getDatabaseHandler().getActivatedProfile();

      boolean isIconResourceID;
      String iconIdentifier;
      String profileName;
      if (profile != null)
      {
        profile.generateIconBitmap(context, 
            GlobalData.applicationWidgetListIconColor.equals("1"), 
            monochromeValue);
        profile.generatePreferencesIndicator(context, 
            GlobalData.applicationWidgetListIconColor.equals("1"), 
            monochromeValue);
        isIconResourceID = profile.getIsIconResourceID();
        iconIdentifier = profile.getIconIdentifier();
        profileName = profile._name;
      }
      else
      {
        // create empty profile and set icon resource
        profile = new Profile();
        profile._name = context.getResources().getString(R.string.profiles_header_profile_name_no_activated);
        profile._icon = GlobalData.PROFILE_ICON_DEFAULT+"|1";

        profile.generateIconBitmap(context, 
            GlobalData.applicationWidgetListIconColor.equals("1"), 
            monochromeValue);
        profile.generatePreferencesIndicator(context, 
            GlobalData.applicationWidgetListIconColor.equals("1"), 
            monochromeValue);
        isIconResourceID = profile.getIsIconResourceID();
        iconIdentifier = profile.getIconIdentifier();
        profileName = profile._name;
      }
          if (isIconResourceID)
          {
            int iconResource = context.getResources().getIdentifier(iconIdentifier, "drawable", context.getPackageName());
            
            widget.setImageViewResource(R.id.widget_profile_list_header_profile_icon, iconResource);
          }
          else
          {
            widget.setImageViewBitmap(R.id.widget_profile_list_header_profile_icon, profile._iconBitmap);
          }
          //if (GlobalData.applicationWidgetListIconColor.equals("1"))
          //{
          red = 0xFF;
          green = 0xFF;
          blue = 0xFF;
          if (GlobalData.applicationWidgetListLightnessT.equals("0")) red = 0x00;
          if (GlobalData.applicationWidgetListLightnessT.equals("25")) red = 0x40;
          if (GlobalData.applicationWidgetListLightnessT.equals("50")) red = 0x80;
          if (GlobalData.applicationWidgetListLightnessT.equals("75")) red = 0xC0;
          if (GlobalData.applicationWidgetListLightnessT.equals("100")) red = 0xFF;
          green = red; blue = red;
            widget.setTextColor(R.id.widget_profile_list_header_profile_name, Color.argb(0xFF, red, green, blue));
          //}
          //else
          //{
      //  widget.setTextColor(R.id.widget_profile_list_header_profile_name, Color.parseColor("#33b5e5"));
          //}
      widget.setTextViewText(R.id.widget_profile_list_header_profile_name, profileName);
      if (GlobalData.applicationWidgetListPrefIndicator)
      {
            widget.setImageViewBitmap(R.id.widget_profile_list_header_profile_pref_indicator, profile._preferencesIndicator);
      }
      if (largeLayout)
      {  
        red = 0xFF;
        green = 0xFF;
        blue = 0xFF;
        if (GlobalData.applicationWidgetListLightnessT.equals("0")) red = 0x00;
        if (GlobalData.applicationWidgetListLightnessT.equals("25")) red = 0x40;
        if (GlobalData.applicationWidgetListLightnessT.equals("50")) red = 0x80;
        if (GlobalData.applicationWidgetListLightnessT.equals("75")) red = 0xC0;
        if (GlobalData.applicationWidgetListLightnessT.equals("100")) red = 0xFF;
        green = red; blue = red;
        widget.setInt(R.id.widget_profile_list_header_separator, "setBackgroundColor", Color.argb(alpha, red, green, blue));
      }
          if (GlobalData.applicationWidgetListIconColor.equals("1"))
          {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_profile_activated);
            bitmap = BitmapManipulator.monochromeBitmap(bitmap, monochromeValue, context);
        widget.setImageViewBitmap(R.id.widget_profile_list_header_profile_activated, bitmap);
          }
          else
          {
            widget.setImageViewResource(R.id.widget_profile_list_header_profile_activated, R.drawable.ic_profile_activated);
          }
    }
    ////////////////////////////////////////////////
    
    // clicks
    if (largeLayout)
    {
      Intent intent = new Intent(context, EditorProfilesActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 
                                      PendingIntent.FLAG_UPDATE_CURRENT);
      widget.setOnClickPendingIntent(R.id.widget_profile_list_header, pendingIntent);

      if (!GlobalData.applicationWidgetListGridLayout)
        widget.setRemoteAdapter(appWidgetId, R.id.widget_profile_list, svcIntent);
      else
        widget.setRemoteAdapter(appWidgetId, R.id.widget_profile_grid, svcIntent);
      
      // The empty view is displayed when the collection has no items. 
          // It should be in the same layout used to instantiate the RemoteViews
          // object above.
      if (!GlobalData.applicationWidgetListGridLayout)
        widget.setEmptyView(R.id.widget_profile_list, R.id.widget_profiles_list_empty);
      else
        widget.setEmptyView(R.id.widget_profile_grid, R.id.widget_profiles_list_empty);
      
      Intent clickIntent=new Intent(context, BackgroundActivateProfileActivity.class);
      clickIntent.putExtra(GlobalData.EXTRA_START_APP_SOURCE, GlobalData.STARTUP_SOURCE_WIDGET);
      PendingIntent clickPI=PendingIntent.getActivity(context, 0,
                                                  clickIntent,
                                                  PendingIntent.FLAG_UPDATE_CURRENT);
            
      if (!GlobalData.applicationWidgetListGridLayout)
        widget.setPendingIntentTemplate(R.id.widget_profile_list, clickPI);
      else
        widget.setPendingIntentTemplate(R.id.widget_profile_grid, clickPI);

    }
    else
    {
      Intent intent = new Intent(context, ActivateProfileActivity.class);
      intent.putExtra(GlobalData.EXTRA_START_APP_SOURCE, GlobalData.STARTUP_SOURCE_WIDGET);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent,  
                            PendingIntent.FLAG_UPDATE_CURRENT);
      widget.setOnClickPendingIntent(R.id.widget_profile_list_header, pendingIntent);
    }
    
    return widget;
  }
  
  public void createProfilesDataWrapper(Context context)
  {
    GlobalData.loadPreferences(context);
    if (dataWrapper == null)
    {
      dataWrapper = new DataWrapper(context, false, false, 0); 
    }
  }
  
  private void doOnUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
  {
    Bundle myOptions;
    if (android.os.Build.VERSION.SDK_INT >= 16)
      myOptions = appWidgetManager.getAppWidgetOptions (appWidgetId);
    else
      myOptions = null;
        setLayoutParams(context, appWidgetManager, appWidgetId, myOptions);
      RemoteViews widget = buildLayout(context, appWidgetManager, appWidgetId, isLargeLayout);      
      appWidgetManager.updateAppWidget(appWidgetId, widget);
  }
  
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
  {
    //Log.e("ProfileListWidgetProvider.onUpdate","xxx");
    
    createProfilesDataWrapper(context);

    for (int i=0; i<appWidgetIds.length; i++)
    {
      doOnUpdate(context, appWidgetManager, appWidgetIds[i]);
    }
          
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    
    if (dataWrapper != null)
      dataWrapper.invalidateDataWrapper();
    dataWrapper = null;
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    String action = intent.getAction();
    
    //Log.e("ProfileListWidgetProvider.onReceive","action="+action);

    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
        AppWidgetManager.INVALID_APPWIDGET_ID);
    
    createProfilesDataWrapper(context);

    if ((action != null) &&
        (action.equalsIgnoreCase("com.motorola.blur.home.ACTION_SET_WIDGET_SIZE")))
    {
      int spanX = intent.getIntExtra("spanX", 1);
      int spanY = intent.getIntExtra("spanY", 1);
      //Log.e("ProfileListWidgetProvider.onReceive","spanX="+spanX);
      //Log.e("ProfileListWidgetProvider.onReceive","spanY="+spanY);
      //Log.e("ProfileListWidgetProvider.onReceive","appWidgetId="+appWidgetId);

      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
          setLayoutParamsMotorola(context, spanX, spanY, appWidgetId);
           RemoteViews layout;
           layout = buildLayout(context, appWidgetManager, appWidgetId, isLargeLayout);
           appWidgetManager.updateAppWidget(appWidgetId, layout);
    }
    else
    if ((action != null) &&
        (action.equalsIgnoreCase(INTENT_REFRESH_LISTWIDGET)))
      updateWidgets(context);
    
    if (dataWrapper != null)
      dataWrapper.invalidateDataWrapper();
    dataWrapper = null;
  }
  
  private void setLayoutParams(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, Bundle newOptions)
  {
    String preferenceKey = "isLargeLayout_"+appWidgetId;
    SharedPreferences preferences = context.getSharedPreferences(GlobalData.APPLICATION_PREFS_NAME, Context.MODE_PRIVATE);

    AppWidgetProviderInfo appWidgetProviderInfo = appWidgetManager.getAppWidgetInfo(appWidgetId); 
    
    int minHeight;
    if (newOptions != null)
    {
      // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
      int category = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
      // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
      isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
  
          //int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
          //int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
          minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
          //int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

      if ((minHeight == 0) && (appWidgetProviderInfo != null))
      {
        minHeight = appWidgetProviderInfo.minHeight;
      }

      //Log.e("ProfileListWidgetProvider.setLayoutParams","minHeight="+minHeight);
      
    }
    else
    {
      isKeyguard = false;
      if (appWidgetProviderInfo != null)
        minHeight = appWidgetProviderInfo.minHeight;
      else
        minHeight = 0;
      //Log.e("ProfileListWidgetProvider.setLayoutParams"," null minHeight="+minHeight);

      //if (minHeight == 0)
      //  return;
    }

        if (isKeyguard)
        {
          if (minHeight < 250) {
              isLargeLayout = false;
          } else {
              isLargeLayout = true;
          }
        }
        else
        {
          if (minHeight < 110) {
              isLargeLayout = false;
          } else {
              isLargeLayout = true;
          }
      }
        
    if (preferences.contains(preferenceKey))
      isLargeLayout = preferences.getBoolean(preferenceKey, true);
    else
    {
      Editor editor = preferences.edit();
      editor.putBoolean(preferenceKey, isLargeLayout);
      editor.commit();
    }
        
  }

  private void setLayoutParamsMotorola(Context context, int spanX, int spanY, int appWidgetId)
  {
    isKeyguard = false;
        if (spanY == 1) {
            isLargeLayout = false;
        } else {
            isLargeLayout = true;
        }
        
    String preferenceKey = "isLargeLayout_"+appWidgetId;
    SharedPreferences preferences = context.getSharedPreferences(GlobalData.APPLICATION_PREFS_NAME, Context.MODE_PRIVATE);

    Editor editor = preferences.edit();
    editor.putBoolean(preferenceKey, isLargeLayout);
    editor.commit();
  }

  
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, Bundle newOptions) 
    {
    //Log.e("ProfileListWidgetProvider.onAppWidgetOptionsChanged","xxx");

    createProfilesDataWrapper(context);
    
    String preferenceKey = "isLargeLayout_"+appWidgetId;
    SharedPreferences preferences = context.getSharedPreferences(GlobalData.APPLICATION_PREFS_NAME, Context.MODE_PRIVATE);
    
    // remove preference, will by reseted in setLayoutParams
    Editor editor = preferences.edit();
    editor.remove(preferenceKey);
    editor.commit();
    
    updateWidget(context, appWidgetId);

    if (dataWrapper != null)
      dataWrapper.invalidateDataWrapper();
    dataWrapper = null;
    }  

  private void updateWidget(Context context, int appWidgetId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

      doOnUpdate(context, appWidgetManager, appWidgetId);
      if (isLargeLayout)
      {
      if (!GlobalData.applicationWidgetListGridLayout)
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_profile_list);
      else
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_profile_grid);
      }
  }  
  
  private void updateWidgets(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
      int appWidgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, ProfileListWidgetProvider.class));

    for (int i=0; i<appWidgetIds.length; i++)
    {
      updateWidget(context, appWidgetIds[i]);
    }
  }  
  
}




Java Source Code List

sk.henrichg.phoneprofiles.ActivateProfileActivity.java
sk.henrichg.phoneprofiles.ActivateProfileHelper.java
sk.henrichg.phoneprofiles.ActivateProfileListAdapter.java
sk.henrichg.phoneprofiles.ActivateProfileListFragment.java
sk.henrichg.phoneprofiles.ApplicationsCache.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceAdapter.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceDialog.java
sk.henrichg.phoneprofiles.ApplicationsPreference.java
sk.henrichg.phoneprofiles.BackgroundActivateProfileActivity.java
sk.henrichg.phoneprofiles.BitmapManipulator.java
sk.henrichg.phoneprofiles.BootUpReceiver.java
sk.henrichg.phoneprofiles.BrightnessDialogPreference.java
sk.henrichg.phoneprofiles.BrightnessView.java
sk.henrichg.phoneprofiles.DashClockBroadcastReceiver.java
sk.henrichg.phoneprofiles.DataWrapper.java
sk.henrichg.phoneprofiles.DatabaseHandler.java
sk.henrichg.phoneprofiles.EditorProfileListAdapter.java
sk.henrichg.phoneprofiles.EditorProfileListFragment.java
sk.henrichg.phoneprofiles.EditorProfilesActivity.java
sk.henrichg.phoneprofiles.ExecuteRadioProfilePrefsService.java
sk.henrichg.phoneprofiles.ExecuteVolumeProfilePrefsService.java
sk.henrichg.phoneprofiles.FirstStartService.java
sk.henrichg.phoneprofiles.GUIData.java
sk.henrichg.phoneprofiles.GlobalData.java
sk.henrichg.phoneprofiles.IconWidgetProvider.java
sk.henrichg.phoneprofiles.ImageViewPreferenceAdapter.java
sk.henrichg.phoneprofiles.ImageViewPreferenceDialog.java
sk.henrichg.phoneprofiles.ImageViewPreference.java
sk.henrichg.phoneprofiles.KeyguardService.java
sk.henrichg.phoneprofiles.Keyguard.java
sk.henrichg.phoneprofiles.LocaleChangedReceiver.java
sk.henrichg.phoneprofiles.NumberPickerPreference.java
sk.henrichg.phoneprofiles.OneRowWidgetProvider.java
sk.henrichg.phoneprofiles.PackageReplacedReceiver.java
sk.henrichg.phoneprofiles.PhoneCallBroadcastReceiver.java
sk.henrichg.phoneprofiles.PhoneCallReceiver.java
sk.henrichg.phoneprofiles.PhoneProfilesDashClockExtension.java
sk.henrichg.phoneprofiles.PhoneProfilesHelper.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesActivity.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesFragment.java
sk.henrichg.phoneprofiles.ProfileDurationAlarmBroadcastReceiver.java
sk.henrichg.phoneprofiles.ProfileListWidgetFactory.java
sk.henrichg.phoneprofiles.ProfileListWidgetProvider.java
sk.henrichg.phoneprofiles.ProfileListWidgetService.java
sk.henrichg.phoneprofiles.ProfilePreferenceAdapter.java
sk.henrichg.phoneprofiles.ProfilePreferenceDialog.java
sk.henrichg.phoneprofiles.ProfilePreference.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragmentActivity.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragment.java
sk.henrichg.phoneprofiles.ProfilePreferencesIndicator.java
sk.henrichg.phoneprofiles.Profile.java
sk.henrichg.phoneprofiles.ReceiversService.java
sk.henrichg.phoneprofiles.RefreshGUIBroadcastReceiver.java
sk.henrichg.phoneprofiles.RemoteExportDataActivity.java
sk.henrichg.phoneprofiles.RemoveBrightnessViewBroadcastReceiver.java
sk.henrichg.phoneprofiles.ScreenOnOffBroadcastReceiver.java
sk.henrichg.phoneprofiles.ShortcutCreatorActivity.java
sk.henrichg.phoneprofiles.ShortcutCreatorListFragment.java
sk.henrichg.phoneprofiles.ShortcutProfileListAdapter.java
sk.henrichg.phoneprofiles.UpgradePPHelperActivity.java
sk.henrichg.phoneprofiles.VolumeDialogPreference.java