package org.smblott.intentradio;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.Context;

import android.os.AsyncTask;;

import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class IntentRadio extends Activity
{
   static Context context = null;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      context = getApplicationContext();
      Logger.init(context);

      setContentView(R.layout.main);

      TextView view = (TextView) findViewById(R.id.text);
      view.setMovementMethod(LinkMovementMethod.getInstance());
      view.setText("Loading...");

      // Read file contents and build date for main screen asyncronously...
      //
      new AsyncTask<TextView, Void, Spanned>()
      {
         TextView view = null;

         @Override
         protected Spanned doInBackground(TextView... v)
         {
            view = v[0];
            String file = ReadRawTextFile.read(getApplicationContext(),R.raw.message);

            String version = getString(R.string.version);
            version = "<p>Version: " + version + "<br>\n";

            String build_date = Build.getBuildDate(context);
            build_date = "Build: " + build_date + "\n</p>\n";

            return Html.fromHtml(file + version + build_date );
         }

         @Override
         protected void onPostExecute(Spanned html)
            { view.setText(html); }

      }.execute(view);
   }

   /* ********************************************************************
    * Launch clip buttons...
    */

   public void clip_buttons(View v)
   {
      Intent clipper = new Intent(IntentRadio.this, ClipButtons.class);
      startActivity(clipper);
   }

   /* ********************************************************************
    * Install sample Tasker project...
    *
    * This currently assumes that Tasker *always* stores projects in:
    *
    *    - /sdcard/Tasker/projects
    *
    * Does it?
    *
    * File I/O is more blocking than anything else we're doing, so we'll do it
    * asyncronously.
    */

   private static final String project_file = "Tasker/projects/IntentRadio.prj.xml";

   public void install_tasker(View v)
   {

      new AsyncTask<Void, Void, String>()
      {
         @Override
         protected String doInBackground(Void... unused)
         {
            return CopyResource.copy(context, R.raw.tasker, project_file);
         }

         @Override
         protected void onPostExecute(String error)
         {
            if ( error == null )
            {
               toast("Project file installed...\n\n/sdcard/" + project_file);
               toast("Now import this project into Tasker.");
            }
            else
               toast("Install error:\n" + error + "\n\n/sdcard/" + project_file);
         }

      }.execute();
   }

   /* ********************************************************************
    * Toasts...
    */

   static private void toast(String msg)
      { Logger.toast_long(msg); }

}

@@ -1123,21 +1123,49 @@
     
-String file =
+return Html.fromHtml(%0A                   
  Rea
@@ -1220,18 +1220,17 @@
 message)
-;%0A
+ 
 %0A       
@@ -1238,24 +1238,32 @@
     
-String v
+      + %22%3Cp%3EV
 ersion
- =
+: %22 +
  get
@@ -1290,56 +1290,8 @@
 ion)
-;%0A            version = %22%3Cp%3EVersion: %22 + version
  + %22
@@ -1293,27 +1293,30 @@
 ) + %22%3Cbr%3E%5Cn%22
-;%0A%0A
+ %0A    
             
@@ -1319,27 +1319,23 @@
     
-String build_date =
+  + %22Build: %22 +
  Bui
@@ -1362,57 +1362,8 @@
 ext)
-;%0A            build_date = %22Build: %22 + build_date
  + %22
@@ -1371,18 +1371,16 @@
 n%3C/p%3E%5Cn%22
-;%0A
 %0A       
@@ -1388,56 +1388,13 @@
     
-return Html.fromHtml(file + version + build_date
+     
  );%0A
@@ -1485,20 +1485,67 @@
         
+%7B%0A
    
-%7B
+         if ( ! isCancelled() )%0A              
  view.se
@@ -1556,16 +1556,25 @@
 t(html);
+%0A        
  %7D%0A%0A    
@@ -2653,32 +2653,268 @@
 ror)%0A         %7B%0A
+            // TODO: Checking whether the task has been cancelled is not%0A            // sufficient.  Must also check whether activity has been%0A            // destroyed.%0A            //%0A            if ( ! isCancelled() )%0A            %7B%0A   
             if (
@@ -2934,32 +2934,35 @@
 l )%0A            
+   
 %7B%0A              
@@ -2958,24 +2958,27 @@
             
+ 
+  
 toast(%22Proje
@@ -3047,16 +3047,19 @@
     
+   
 toast(%22N
 ow i
@@ -3058,10 +3058,12 @@
 t(%22N
-ow
+ext,
  imp
@@ -3107,18 +3107,24 @@
         
-%7D%0A
+   %7D%0A   
         
@@ -3143,24 +3143,27 @@
             
+   
 toast(%22Insta
@@ -3209,32 +3209,46 @@
  project_file);%0A
+            %7D%0A
          %7D%0A%0A    

Check tasks cancelled.