package com.example.ar;

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ThankYouActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thank_you);

        final Resources res = getResources();
        final TextView label = (TextView) findViewById(R.id.label);
        label.setText(res.getString(R.string.wait));
        /*
         * Worker thread using a Handler to update the UI thread
         */
        final Handler handler = new Handler();
        // Start lengthy operation in a background thread
        Thread thread = new Thread(new Runnable() {
            public void run() {
                Random rand = new Random();
                for (int i = 0; i < 50; i++) {
                    try {
                        int amt = (int) (rand.nextFloat() * 100);
                        Thread.sleep(amt);
                    }
                    catch (Exception e) {
                        System.err.println(e);
                    }

                    // Update the progress bar using a Handler
                    handler.post(new Runnable() {
                        public void run() {
                            ProgressBar bar = (ProgressBar) 
                                    findViewById(R.id.progress_bar);
                            bar.setProgress(bar.getProgress() + 1);
                            if (bar.getProgress() == bar.getMax()) {
                                bar.setVisibility(View.INVISIBLE);
                                label.setText(res
                                        .getString(R.string.thanks));
                            }
                        }
                    });
                }
            }
        });
        thread.start();
        /*
         * Worker thread using a AsyncTask to update the UI thread
         */
/*        
        class MyAsyncTask extends AsyncTask<Random, Integer, String> {
            private ProgressBar bar;

            @Override
            protected void onPreExecute() {
                bar = (ProgressBar) findViewById(R.id.progress_bar);
            }

            @Override
            protected String doInBackground(Random... params) {
                for (int i = 0; i < 50; i++) {
                    try {
                        int amt = (int) (params[0].nextFloat() * 100);
                        Thread.sleep(amt);
                    }
                    catch (Exception e) {
                        System.err.println(e);
                    }
                    publishProgress(i + 1);
                }
                return res.getString(R.string.thanks);
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                bar.setProgress(values[0]);
            }

            @Override
            protected void onPostExecute(String result) {
                bar.setVisibility(View.INVISIBLE);
                label.setText(result);
            }
        }
        new MyAsyncTask().execute(new Random());
*/
    }
}