Small style stuff
[zxing.git] / android / src / com / google / zxing / client / android / wifi / Killer.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.android.wifi;
18
19 import java.util.Timer;
20 import java.util.TimerTask;
21
22 import com.google.zxing.client.android.R;
23
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.content.ActivityNotFoundException;
27 import android.content.Intent;
28 import android.net.Uri;
29 import android.os.Handler;
30
31 /**
32  * Close the parent after a delay.
33  * @author Vikram Aggarwal
34  */
35 final class Killer implements Runnable {
36
37   // Three full seconds
38   private static final long DELAY_MS = 3 * 1000L;
39
40   private final Activity parent;
41
42   Killer(Activity parent) {
43     this.parent = parent;
44   }
45   void launchIntent(Intent intent) {
46     if (intent != null) {
47       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
48       try {
49         parent.startActivity(intent);
50       } catch (ActivityNotFoundException e) {
51         AlertDialog.Builder builder = new AlertDialog.Builder(parent);
52         builder.setTitle(R.string.app_name);
53         builder.setMessage(R.string.msg_intent_failed);
54         builder.setPositiveButton(R.string.button_ok, null);
55         builder.show();
56       }
57     }
58   }
59
60   public void run() {
61     final Handler handler = new Handler();
62     Timer t = new Timer();
63     t.schedule(new TimerTask() {
64       @Override
65       public void run() {
66         handler.post(new Runnable() {
67           public void run() {
68             // This will kill the parent, a bad idea.
69 //            parent.finish();
70             // This will start the browser, a better idea
71             launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
72           }
73         });
74       }
75     }, DELAY_MS);
76   }
77 }