/**
 * This file is part of the Android DependencyManager project hosted at
 * http://code.google.com/p/android-dependencymanager/
 *
 * Copyright (C) 2009 Jens Finkhaeuser <jens@finkhaeuser.de>
 *
 * 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 de.finkhaeuser.dm.testapp;

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;

import android.content.Intent;

import android.database.Cursor;
import android.database.ContentObserver;
import android.database.DataSetObserver;

import android.net.Uri;

import java.util.LinkedList;

import de.finkhaeuser.dm.common.DependencyManagerContract;
import de.finkhaeuser.dm.common.DependencyManagerContract.DependencyColumns;
import de.finkhaeuser.dm.common.Intents;

import de.finkhaeuser.dm.client.DependencyManager;

import android.util.Log;


public class TestApp extends Activity implements DependencyManager.BindListener
{
  public static final String LTAG = "TestApp";

  private DependencyManager mDependencyManager;

  class Foo extends ContentObserver
  {
    Cursor mCursor;
    public Foo(Cursor c)
    {
      super(new Handler());
      mCursor = c;
    }

    public void onChange(boolean selfChange)
    {
      Log.d(LTAG, "on change");
      mCursor.requery();
      Log.d(LTAG, "size after requery: " + mCursor.getCount());
      mCursor.moveToFirst();
      do {
        String pkg = mCursor.getString(mCursor.getColumnIndex(
              DependencyColumns.APP_PACKAGE));
        Log.d(LTAG, "package: " + pkg);
        mCursor.moveToNext();
      } while (!mCursor.isAfterLast());

    }
  }


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create an Intent that we'll query for. XXX This is manual now, but should
    // be read from the mandatory intents in the longer term.
    LinkedList<Intent> il = new LinkedList<Intent>();

    Intent i = new Intent();
    i.setAction("dummyAction");
    i.addCategory("dummyCategory");
    il.add(i);
    i = new Intent();
    i.setAction("action2");
    i.addCategory("cat2");
    il.add(i);

    Uri uri = Uri.parse(String.format("content://%s/%s?%s",
            DependencyManagerContract.CONTENT_AUTHORITY,
            DependencyManagerContract.PATH_LIST_CANDIDATES,
            Intents.serializeIntents(il)));

    // XXX This'll need to change. It's just to launch the DpendencyManager
    // right now.
    Cursor managedCursor = managedQuery(uri,
        DependencyColumns.CANDIDATE_PROJECTION,
        null,          // WHERE clause.
        null,          // WHERE clause value substitution
        null);   // Sort order.

    Log.d(LTAG, "count: " + managedCursor.getCount());
    managedCursor.registerContentObserver(new Foo(managedCursor));

    if (0 < managedCursor.getCount()) {
      managedCursor.moveToFirst();
      do {
        String pkg = managedCursor.getString(managedCursor.getColumnIndex(
              DependencyColumns.APP_PACKAGE));
        Log.d(LTAG, "package: " + pkg);
        managedCursor.moveToNext();
      } while (!managedCursor.isAfterLast());
    }


    // Test service!
    DependencyManager.bindService(this, this);
  }



  public void onBound(DependencyManager dm)
  {
    mDependencyManager = dm;
    mDependencyManager.resolveDependencies(getPackageName());
  }



  @Override
  public void onStop()
  {
    super.onStop();
    mDependencyManager.unbindService();
  }

}

@@ -916,2792 +916,451 @@
 ort 
-android.database.Cursor;%0Aimport android.database.ContentObserver;%0Aimport android.database.DataSetObserver;%0A%0Aimport android.net.Uri;%0A%0Aimport java.util.LinkedList;%0A%0Aimport de.finkhaeuser.dm.common.DependencyManagerContract;%0Aimport de.finkhaeuser.dm.common.DependencyManagerContract.DependencyColumns;%0Aimport de.finkhaeuser.dm.common.Intents;%0A%0Aimport de.finkhaeuser.dm.client.DependencyManager;%0A%0Aimport android.util.Log;%0A%0A%0Apublic class TestApp extends Activity implements DependencyManager.BindListener%0A%7B%0A  public static final String LTAG = %22TestApp%22;%0A%0A  private DependencyManager mDependencyManager;%0A%0A  class Foo extends ContentObserver%0A  %7B%0A    Cursor mCursor;%0A    public Foo(Cursor c)%0A    %7B%0A      super(new Handler());%0A      mCursor = c;%0A    %7D%0A%0A    public void onChange(boolean selfChange)%0A    %7B%0A      Log.d(LTAG, %22on change%22);%0A      mCursor.requery();%0A      Log.d(LTAG, %22size after requery: %22 + mCursor.getCount());%0A      mCursor.moveToFirst();%0A      do %7B%0A        String pkg = mCursor.getString(mCursor.getColumnIndex(%0A              DependencyColumns.APP_PACKAGE));%0A        Log.d(LTAG, %22package: %22 + pkg);%0A        mCursor.moveToNext();%0A      %7D while (!mCursor.isAfterLast());%0A%0A    %7D%0A  %7D%0A%0A%0A  /** Called when the activity is first created. */%0A  @Override%0A  public void onCreate(Bundle savedInstanceState)%0A  %7B%0A    super.onCreate(savedInstanceState);%0A    setContentView(R.layout.main);%0A%0A    // Create an Intent that we'll query for. XXX This is manual now, but should%0A    // be read from the mandatory intents in the longer term.%0A    LinkedList%3CIntent%3E il = new LinkedList%3CIntent%3E();%0A%0A    Intent i = new Intent();%0A    i.setAction(%22dummyAction%22);%0A    i.addCategory(%22dummyCategory%22);%0A    il.add(i);%0A    i = new Intent();%0A    i.setAction(%22action2%22);%0A    i.addCategory(%22cat2%22);%0A    il.add(i);%0A%0A    Uri uri = Uri.parse(String.format(%22content://%25s/%25s?%25s%22,%0A            DependencyManagerContract.CONTENT_AUTHORITY,%0A            DependencyManagerContract.PATH_LIST_CANDIDATES,%0A            Intents.serializeIntents(il)));%0A%0A    // XXX This'll need to change. It's just to launch the DpendencyManager%0A    // right now.%0A    Cursor managedCursor = managedQuery(uri,%0A        DependencyColumns.CANDIDATE_PROJECTION,%0A        null,          // WHERE clause.%0A        null,          // WHERE clause value substitution%0A        null);   // Sort order.%0A%0A    Log.d(LTAG, %22count: %22 + managedCursor.getCount());%0A    managedCursor.registerContentObserver(new Foo(managedCursor));%0A%0A    if (0 %3C managedCursor.getCount()) %7B%0A      managedCursor.moveToFirst();%0A      do %7B%0A        String pkg = managedCursor.getString(managedCursor.getColumnIndex(%0A              DependencyColumns.APP_PACKAGE));%0A        Log.d(LTAG, %22package: %22 + pkg);%0A        managedCursor.moveToNext();%0A      %7D while (!managedCursor.isAfterLast());%0A    %7D
+de.finkhaeuser.dm.client.DependencyManager;%0A%0Aimport android.util.Log;%0A%0A%0Apublic class TestApp extends Activity implements DependencyManager.BindListener%0A%7B%0A  public static final String LTAG = %22TestApp%22;%0A%0A  private DependencyManager mDependencyManager;%0A%0A  /** Called when the activity is first created. */%0A  @Override%0A  public void onCreate(Bundle savedInstanceState)%0A  %7B%0A    super.onCreate(savedInstanceState);%0A    setContentView(R.layout.main);
 %0A%0A%0A 

Simplify TestApp; just uses DependencyManager interface now.