Android Open Source - cicada Widget Setup






From Project

Back to project page cicada.

License

The source code is released under:

Apache License

If you think the Android project cicada 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) 2011 Cicada contributors
////  w ww  . j  av  a 2 s . c  o  m
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package org.cicadasong.cicada;

package org.cicadasong.cicada;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.cicadasong.cicadalib.CicadaApp.AppType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * This Activity presents the UI that allows the user to configure which installed widgets are
 * displayed on which parts on the widget screen.
 */

public class WidgetSetup extends Activity {
  public static final String INTENT_WIDGETS_CHANGED = "org.cicadasong.cicada.WIDGETS_CHANGED";
  
  public static final AppDescription NONE =
      new AppDescription("NONE", "NONE", null, "(None)", AppType.NONE);
  
  public static final int TOP_WIDGET = 0;
  public static final int MIDDLE_WIDGET = 1;
  public static final int BOTTOM_WIDGET = 2;
  
  private List<AppDescription> widgets;
  private AppDescription[] selections;
  private AppDescription[] lastDBSelections = new AppDescription[WidgetScreen.NUM_WIDGETS];
  
  private Spinner[] spinners;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widget_setup);
    
    AppDatabase db = new AppDatabase(this);
    widgets = db.getWidgets();
    widgets.add(0, NONE);

    selections = db.getWidgetSetup().toArray(new AppDescription[]{});
    for (int i = 0; i < selections.length; i++) {
      if ((selections[i] == null) || (indexForApp(selections[i]) == 0)) {
        selections[i] = NONE;
      }
    }
    System.arraycopy(selections, 0, lastDBSelections, 0, selections.length);
    
    db.close();
    
    ArrayAdapter<AppDescription> widgetAdapter =
        new ArrayAdapter<AppDescription>(this, R.layout.spinner_item, widgets);
    
    spinners = new Spinner[WidgetScreen.NUM_WIDGETS];
    for (int i = 0; i < spinners.length; i++) {
      Spinner spinner;
      switch (i) {
      case TOP_WIDGET:
      default:
        spinner = (Spinner) findViewById(R.id.top_widget_spinner);
        break;
      case MIDDLE_WIDGET:
        spinner = (Spinner) findViewById(R.id.middle_widget_spinner);
        break;
      case BOTTOM_WIDGET:
        spinner = (Spinner) findViewById(R.id.bottom_widget_spinner);
        break;
      }
      spinners[i] = spinner;
      spinner.setAdapter(widgetAdapter);
      final int spinnerId = i;
      spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
          widgetSelected(spinnerId, widgets.get(pos));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
      });
      
    }
    updateSpinners();
  }
  
  private void widgetSelected(int spinner, AppDescription app) {
    for (int i = 0; i < selections.length; i++) {
      if (i == spinner) {
        continue;
      }
      if (selections[i].equals(app)) {
        selections[i] = NONE;
      }
    }
    selections[spinner] = app;
    updateSpinners();
    
    if (!Arrays.equals(selections, lastDBSelections)) {
      saveSetup();
    }
  }

  private void saveSetup() {
    System.arraycopy(selections, 0, lastDBSelections, 0, selections.length);
    List<AppDescription> storeWidgets = new ArrayList<AppDescription>();
    for (int i = 0; i < selections.length; i++) {
      if (selections[i] == NONE) {
        storeWidgets.add(null);
      } else {
        storeWidgets.add(selections[i]);
      }
    }
    
    AppDatabase db = new AppDatabase(this);
    db.setWidgetSetup(storeWidgets);
    db.close();
    
    sendBroadcast(new Intent(INTENT_WIDGETS_CHANGED));
  }
  
  private void updateSpinners() {
    for (int i = 0; i < spinners.length; i++) {
      spinners[i].setSelection(indexForApp(selections[i]));
    }
  }
  
  private int indexForApp(AppDescription app) {
    return Math.max(0, widgets.indexOf(app));
  }
}




Java Source Code List

com.hydraproductions.cicada.StopWatch.java
org.cicadasong.apollo.ApolloConfig.java
org.cicadasong.apollo.ApolloIntents.java
org.cicadasong.apollo.BitmapUtil.java
org.cicadasong.apollo.SimulatedDisplayView.java
org.cicadasong.cicada.AppDatabase.java
org.cicadasong.cicada.AppDescription.java
org.cicadasong.cicada.AppList.java
org.cicadasong.cicada.AppScanner.java
org.cicadasong.cicada.CicadaService.java
org.cicadasong.cicada.CicadaSettingsActivity.java
org.cicadasong.cicada.Cicada.java
org.cicadasong.cicada.DeviceService.java
org.cicadasong.cicada.HotkeySetupActivity.java
org.cicadasong.cicada.HotkeySetupEntry.java
org.cicadasong.cicada.MetaWatchConnection.java
org.cicadasong.cicada.NotificationRenderer.java
org.cicadasong.cicada.PackageMonitor.java
org.cicadasong.cicada.PackageUtil.java
org.cicadasong.cicada.PrefUtil.java
org.cicadasong.cicada.WidgetScreen.java
org.cicadasong.cicada.WidgetSetup.java
org.cicadasong.cicadalib.CicadaApp.java
org.cicadasong.cicadalib.CicadaIntents.java
org.cicadasong.cicadalib.CicadaNotificationManager.java
org.cicadasong.cicadalib.CicadaNotification.java
org.cicadasong.cicadalib.TextBlock.java
org.cicadasong.samples.bitly.Bitly.java
org.cicadasong.samples.digitalclock.DigitalClock.java
org.cicadasong.samples.hellocicada.HelloCicada.java
org.cicadasong.samples.nextbuses.NextBuses.java
org.cicadasong.samples.notificationdemo.NotificationDemo.java
org.cicadasong.samples.notifications.NotificationAccessibilityService.java
org.cicadasong.samples.notifications.Notifications.java
org.cicadasong.samples.quicktext.QuickTextSetup.java
org.cicadasong.samples.quicktext.QuickText.java
org.cicadasong.samples.smsnotifier.SMSBroadcastReceiver.java
org.cicadasong.samples.tubestatus.Preferences.java
org.cicadasong.samples.tubestatus.TubeStatusSettingsActivity.java
org.cicadasong.samples.tubestatus.TubeStatus.java
org.cicadasong.samples.webimageplayer.Preferences.java
org.cicadasong.samples.webimageplayer.WebImagePlayerSettingsActivity.java
org.cicadasong.samples.webimageplayer.WebImagePlayer.java