package org.gingercal; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.ScrollView; import org.gingercal.Model.Cal; /** Pick which calendars to show and which one new events go to. */ public class CalendarsActivity extends Activity { final List cals = new ArrayList(); final List boxes = new ArrayList(); Button defBtn; @Override protected void onCreate(Bundle b) { super.onCreate(b); setTitle(L.t("Calendars")); cals.addAll(Db.get(this).calendarList()); LinearLayout f = App.vbox(this); int p = App.dp(this, 8); f.setPadding(p, p, p, p); if (cals.isEmpty()) { f.addView(App.text(this, L.t("No calendars yet. Set up the account in Settings and sync."), 14, 0xFFFFFFFF)); } for (Cal c : cals) { LinearLayout row = new LinearLayout(this); row.setOrientation(LinearLayout.HORIZONTAL); View bar = new View(this); bar.setBackgroundColor(c.color); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(App.dp(this, 6), -1); lp.setMargins(0, App.dp(this, 6), App.dp(this, 4), App.dp(this, 6)); row.addView(bar, lp); CheckBox cb = new CheckBox(this); cb.setText(c.name + (c.writable ? "" : " " + L.t("(read-only)"))); cb.setChecked(c.enabled); row.addView(cb, new LinearLayout.LayoutParams(-1, -2)); boxes.add(cb); f.addView(row, new LinearLayout.LayoutParams(-1, -2)); } defBtn = new Button(this); defBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { pickDefault(); } }); if (!cals.isEmpty()) f.addView(defBtn, new LinearLayout.LayoutParams(-1, -2)); ScrollView sv = new ScrollView(this); sv.addView(f); setContentView(sv); label(); } void label() { String def = App.get(this, "defcal"); String name = L.t("first writable"); for (Cal c : cals) if (c.href.equals(def)) name = c.name; defBtn.setText(L.f("New events go to: %s", name)); } void pickDefault() { final List w = new ArrayList(); for (Cal c : cals) if (c.writable) w.add(c); String[] names = new String[w.size()]; for (int i = 0; i < names.length; i++) names[i] = w.get(i).name; new AlertDialog.Builder(this).setTitle(L.t("Default calendar")) .setItems(names, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int which) { App.prefs(CalendarsActivity.this).edit().putString("defcal", w.get(which).href).commit(); label(); } }).show(); } @Override protected void onPause() { super.onPause(); boolean changed = false; for (int i = 0; i < cals.size(); i++) { boolean on = boxes.get(i).isChecked(); if (on != cals.get(i).enabled) { Db.get(this).setEnabled(cals.get(i).href, on); cals.get(i).enabled = on; changed = true; } } if (changed) App.requestSync(this, true, false); } }