カウントダウンな Dialog

検証名目で微妙なソレがでっち上がったので晒してみる事に。

package com.example.erapsedtimetestactivity;

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

import android.app.Activity;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static class MyCustomDialog extends Dialog {

        private Handler mHandler = new Handler();
        private int mShowTime;
        private Runnable mRunDismiss;
        private Runnable mRunCountdown;
        private int mCount;

        public MyCustomDialog(Context context, int theme) {
            super(context, theme);
            init();
            this.setContentView(R.layout.custom_dialog);
        }
        public MyCustomDialog(Context context) {
            super(context);
            init();
            this.setContentView(R.layout.custom_dialog);
        }

        public void setShowTime(int time) {
            this.mShowTime = time;
            this.mCount = time / 1000;
        }
        
        private void init() {
            getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
         
        @Override
        public void show() {
            final MyCustomDialog dlg = this;
             
            this.mRunDismiss = new Runnable() {
                public void run() {
                    dlg.dismiss();
                }
            };

            this.mRunCountdown = new Runnable() {
                public void run() {
                    dlg.updateCountdown();

                    dlg.mHandler.postDelayed(dlg.mRunCountdown, 1000);
                }
            };

            this.mHandler.postDelayed(this.mRunDismiss, this.mShowTime);
            this.mHandler.postDelayed(this.mRunCountdown, 1000);
             
            super.show();
        }
     
        public void updateCountdown() {
            this.mCount--;
            TextView textView = (TextView)this.findViewById(R.id.textView1);
            textView.setText("   送信中" + "\n\n" + 
                             "送信終了まであと " + this.mCount + " 秒");
        }

    }
    
    public static class MyProgressDialog extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            MyCustomDialog dialog = new MyCustomDialog(getActivity());        
            
            dialog.setShowTime(10000);

            return dialog;
        }
        
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                FragmentManager manager = getFragmentManager();  
                final MyProgressDialog dialog = new MyProgressDialog();

                dialog.show(manager, "dialog");
            }
            
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

でもなんとなく何もできない (cancelable な) ProgressDialog あたりが時間カウントして自動で dismiss されちゃう方が色々な意味で自然な気がしてたりして。
おそらく別途エントリ入れるか追加な方向になるかと。

ちなみに

下記エントリを参考にさせて頂いております。ありがとうございます。

つうか

これ、ずっと処理待ちにしといても例の例外は発生せんのかな。