コード例 #1
0
  private void updateWidgetContent(
      Context context,
      AppWidgetManager appWidgetManager,
      int[] widgetIds,
      RemoteViews remoteViews) {

    Log.d(TAG, "Updating TodoWidgetProvider content.");

    // get widget ID's if not provided
    if (widgetIds == null) {
      widgetIds =
          appWidgetManager.getAppWidgetIds(
              new ComponentName(context, TodoWidgetProvider.class.getName()));
    }

    // get remoteViews if not provided
    if (remoteViews == null) {
      remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    }

    // get taskBag from application
    TaskBag taskBag = ((TodoApplication) ((ContextWrapper) context).getBaseContext()).getTaskBag();

    List<Task> tasks = taskBag.getTasks();
    int taskCount = tasks.size();
    Resources resources = context.getResources();

    for (int i = 0; i < TASKS_TO_DISPLAY; i++) {
      // get task to display
      if (i >= tasks.size()) {
        // no more tasks to display
        remoteViews.setViewVisibility(id[i][TASK_ID], View.GONE);
        remoteViews.setViewVisibility(id[i][TASK_PRIO], View.GONE);
        remoteViews.setViewVisibility(id[i][TASK_TEXT], View.GONE);
        continue;
      }
      Task task = tasks.get(i);

      if (!task.isCompleted()) { // don't show completed tasks
        // text
        String taskText;
        if (task.inScreenFormat().length() > 33) {
          taskText = task.inScreenFormat().substring(0, 33) + "...";
        } else {
          taskText = task.inScreenFormat();
        }
        SpannableString ss = new SpannableString(taskText);
        remoteViews.setTextViewText(id[i][TASK_TEXT], ss);
        remoteViews.setViewVisibility(id[i][TASK_TEXT], View.VISIBLE);

        // priority
        int color = R.color.white;
        switch (task.getPriority()) {
          case A:
            color = R.color.green;
            break;
          case B:
            color = R.color.blue;
            break;
          case C:
            color = R.color.orange;
            break;
          case D:
            color = R.color.gold;
          default:
            break;
        }

        remoteViews.setTextViewText(id[i][TASK_PRIO], task.getPriority().inListFormat());
        remoteViews.setTextColor(id[i][TASK_PRIO], resources.getColor(color));
        remoteViews.setViewVisibility(id[i][TASK_PRIO], View.VISIBLE);
      }
    }

    remoteViews.setViewVisibility(R.id.empty, taskCount == 0 ? View.VISIBLE : View.GONE);

    appWidgetManager.updateAppWidget(widgetIds, remoteViews);
  }
コード例 #2
0
ファイル: AddTask.java プロジェクト: boliveira/todo.txt-touch
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.add_task);

    ArrayList<Task> tasks;
    try {
      tasks = TodoUtil.loadTasksFromFile();
    } catch (IOException e) {
      Log.e(TAG, e.getMessage(), e);
      tasks = new ArrayList<Task>();
    }

    final Intent intent = getIntent();
    final String action = intent.getAction();
    // create shortcut and exit
    // create shortcut and exit
    if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
      Log.d(TAG, "Setting up shortcut icon");
      setupShortcut();
      finish();
      return;
    } else if (Intent.ACTION_SEND.equals(action)) {
      Log.d(TAG, "Share");
      share_text = (String) intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
      Log.d(TAG, share_text);
    }

    m_app = (TodoApplication) getApplication();
    // title bar label
    titleBarLabel = (TextView) findViewById(R.id.title_bar_label);

    // text
    final EditText textInputField = (EditText) findViewById(R.id.taskText);
    textInputField.setGravity(Gravity.TOP);

    if (share_text != null) {
      textInputField.setText(share_text);
    }

    Task task = (Task) getIntent().getSerializableExtra(Constants.EXTRA_TASK);
    if (task != null) {
      m_backup = task;
      textInputField.setText(task.inFileFormat());
      setTitle(R.string.update);
      titleBarLabel.setText(R.string.update);
    } else {
      setTitle(R.string.addtask);
      titleBarLabel.setText(R.string.addtask);
    }

    textInputField.setSelection(textInputField.getText().toString().length());

    // priorities
    priorities = (Spinner) findViewById(R.id.priorities);
    final ArrayList<String> prioArr = new ArrayList<String>();
    prioArr.add("Priority");
    prioArr.addAll(Priority.rangeInCode(Priority.A, Priority.E));
    priorities.setAdapter(Util.newSpinnerAdapter(this, prioArr));
    if (m_backup != null) {
      int index = prioArr.indexOf(m_backup.getPriority().getCode());
      priorities.setSelection(index < 0 ? 0 : index);
    }
    priorities.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
            int cursorPosition = textInputField.getSelectionStart();
            String currentText = textInputField.getText().toString();
            Priority priority = Priority.NONE;
            if (position > 0) {
              String item = prioArr.get(position);
              char p = item.charAt(0);
              priority = Priority.toPriority(p);
            }
            String text = PriorityTextSplitter.getInstance().split(currentText).text;
            textInputField.setText(Strings.insertPadded(text, 0, priority.inFileFormat()));
            textInputField.setSelection(
                CursorPositionCalculator.calculate(
                    cursorPosition, currentText, textInputField.getText().toString()));
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {}
        });

    // projects
    projects = (Spinner) findViewById(R.id.projects);
    final ArrayList<String> projectsArr = TaskHelper.getProjects(tasks);
    projectsArr.add(0, "Project");
    projects.setAdapter(Util.newSpinnerAdapter(this, projectsArr));
    projects.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
            if (position > 0) {
              int cursorPosition = textInputField.getSelectionStart();
              String currentText = textInputField.getText().toString();
              String item = "+" + projectsArr.get(position);
              textInputField.setText(Strings.insertPadded(currentText, cursorPosition, item));
              textInputField.setSelection(
                  CursorPositionCalculator.calculate(
                      cursorPosition, currentText, textInputField.getText().toString()));
            }
            projects.setSelection(0);
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {}
        });

    // contexts
    contexts = (Spinner) findViewById(R.id.contexts);
    final ArrayList<String> contextsArr = TaskHelper.getContexts(tasks);
    contextsArr.add(0, "Context");
    contexts.setAdapter(Util.newSpinnerAdapter(this, contextsArr));
    contexts.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
            if (position > 0) {
              int cursorPosition = textInputField.getSelectionStart();
              String currentText = textInputField.getText().toString();
              String item = "@" + contextsArr.get(position);
              textInputField.setText(Strings.insertPadded(currentText, cursorPosition, item));
              textInputField.setSelection(
                  CursorPositionCalculator.calculate(
                      cursorPosition, currentText, textInputField.getText().toString()));
            }
            contexts.setSelection(0);
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {}
        });

    // cancel
    Button cancel = (Button) findViewById(R.id.cancel);
    cancel.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            finish();
          }
        });

    // add
    Button addBtn = (Button) findViewById(R.id.addTask);
    if (m_backup != null) {
      addBtn.setText(R.string.update);
    }
    addBtn.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            // strip line breaks
            final String input =
                textInputField.getText().toString().replaceAll("\\r\\n|\\r|\\n", " ");

            TodoApplication app = (TodoApplication) getApplication();
            DropboxAPI api = app.getAPI();
            new AsyncTask<Object, Void, Boolean>() {
              protected void onPreExecute() {
                m_ProgressDialog =
                    ProgressDialog.show(AddTask.this, getTitle(), "Please wait...", true);
              }

              @Override
              protected Boolean doInBackground(Object... params) {
                try {
                  DropboxAPI api = (DropboxAPI) params[0];
                  Task task = (Task) params[1];
                  String input = (String) params[2];
                  TodoApplication m_app = (TodoApplication) params[3];
                  if (api != null) {
                    if (task != null) {
                      task.update(input);
                      return m_app.m_util.updateTask(task);
                    } else {
                      return m_app.m_util.addTask(input);
                    }
                  }
                } catch (Exception e) {
                  Log.e(TAG, "input: " + input + " - " + e.getMessage());
                }
                return false;
              }

              protected void onPostExecute(Boolean result) {
                if (result) {
                  String res =
                      m_backup != null
                          ? getString(R.string.updated_task)
                          : getString(R.string.added_task);
                  Util.showToastLong(AddTask.this, res);
                  finish();
                } else {
                  String res =
                      m_backup != null
                          ? getString(R.string.update_task_failed)
                          : getString(R.string.add_task_failed);
                  Util.showToastLong(AddTask.this, res);
                }
              }
            }.execute(api, m_backup, input, m_app);
          }
        });
  }