/*
 * @(#)ServiceSelectorXlet.java 
 *
 * Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license
 * to use, modify and redistribute this software in source and binary
 * code form, provided that i) this copyright notice and license appear
 * on all copies of the software; and ii) Licensee does not utilize the
 * software in a manner which is disparaging to Sun.
 */

import javax.tv.locator.*;
import javax.tv.service.*;
import javax.tv.service.navigation.*;
import javax.tv.service.selection.*;
import javax.tv.xlet.*;

public class ServiceSelectorXlet implements Xlet, 
                                 ServiceContextListener {

  ServiceContextFactory scf;
  ServiceContext sc;

  public void initXlet(XletContext context) 
  {  
     scf = ServiceContextFactory.getInstance();
     try { 
        sc = scf.getServiceContext(context);
     } catch (Exception e) {}
  }

  public void pauseXlet() {}
  public void destroyXlet( boolean unconditional ) {}
  public void startXlet()
  { 
     if (sc == null) { // if failed to get a sc at initXlet
        try {
           // get all existing sc
           ServiceContext[] ctxs = scf.getServiceContexts();
           if (ctxs.length > 0) {
              sc = ctxs[0];
           } else {
              // none available, try to create one then.
              sc = scf.createServiceContext();
           }
        } catch (Exception e) { 
           System.out.println("Cannot obtain a valid ServiceContext: " + e);
           return;
        }
     }

     // adding ServiceContextListener
     sc.addListener(this);  
    
     // get all available Services from SIManager
     ServiceList list = SIManager.createInstance().filterServices(null);
     
     // iterate the list to tune
     for (int i = 0; i < list.size(); i++ ) {
        try {
           Service s = list.getService(i);
           System.out.println("selecting: " + s.getName());
           sc.select(s);
           Thread.sleep(20000);
        } catch (Exception e) { 
           System.out.println("selection failed: " + e);
        }
     }

     System.out.println("End of startXlet()");
  }

  // invoked when selection completes
  public void receiveServiceContextEvent(ServiceContextEvent event)
  {
     // selection success
     if (event instanceof PresentationChangedEvent) {
        Service currentService = event.getServiceContext().getService();
        System.out.println("Selection succeeded for: " 
                            + currentService.getName()); 

     // selection terminated
     } else if (event instanceof PresentationTerminatedEvent) {
        int reason = ((PresentationTerminatedEvent)event).getReason();
        System.out.println("Selection terminated with a reason: " 
                            + reason);
    
     // selection failed
     } else if (event instanceof SelectionFailedEvent) {
        int reason = ((SelectionFailedEvent)event).getReason();
        System.out.println("Selection failed with a reason: " 
                            + reason);
     }
  }
}
