Keep an android app up for demo
Revision | ebd8b52d67dd2a376c99520f13ae1ef021569d33 (tree) |
---|---|
Time | 2014-01-30 08:06:06 |
Author | gull08 (HMML) <hmml3939@gmai...> |
Commiter | gull08 (HMML) |
Add setting UI, error toast.
@@ -1,4 +1,11 @@ | ||
1 | 1 | eclipse.preferences.version=1 |
2 | +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |
2 | 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 |
4 | +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |
3 | 5 | org.eclipse.jdt.core.compiler.compliance=1.6 |
6 | +org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |
7 | +org.eclipse.jdt.core.compiler.debug.localVariable=generate | |
8 | +org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |
9 | +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |
10 | +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |
4 | 11 | org.eclipse.jdt.core.compiler.source=1.6 |
@@ -28,15 +28,10 @@ | ||
28 | 28 | <category android:name="android.intent.category.LAUNCHER" /> |
29 | 29 | </intent-filter> |
30 | 30 | </activity> |
31 | - <service android:name="WatchService"/><receiver android:name="StartReceiver"> | |
31 | + <service android:name="WatchService"/><receiver android:name="EventReceiver"> | |
32 | 32 | <intent-filter> |
33 | 33 | <action android:name="android.intent.action.BOOT_COMPLETED" /> |
34 | 34 | <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> |
35 | - </intent-filter> | |
36 | - </receiver> | |
37 | - | |
38 | - <receiver android:name="StopReceiver"> | |
39 | - <intent-filter> | |
40 | 35 | <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> |
41 | 36 | </intent-filter> |
42 | 37 | </receiver> |
@@ -8,86 +8,161 @@ | ||
8 | 8 | android:paddingTop="@dimen/activity_vertical_margin" |
9 | 9 | tools:context=".MainActivity" > |
10 | 10 | |
11 | - <RadioGroup | |
12 | - android:id="@+id/radioGroupEnabled" | |
13 | - android:layout_width="wrap_content" | |
14 | - android:layout_height="wrap_content" | |
15 | - android:layout_alignLeft="@+id/textView1" | |
16 | - android:layout_below="@+id/textView1" | |
17 | - android:orientation="horizontal" > | |
18 | - | |
19 | - <RadioButton | |
20 | - android:id="@+id/radio_enabled" | |
21 | - android:layout_width="wrap_content" | |
22 | - android:layout_height="wrap_content" | |
23 | - android:checked="true" | |
24 | - android:text="Enabled" /> | |
25 | - | |
26 | - <RadioButton | |
27 | - android:id="@+id/radio_disabled" | |
28 | - android:layout_width="wrap_content" | |
29 | - android:layout_height="wrap_content" | |
30 | - android:text="Disabled" /> | |
31 | - </RadioGroup> | |
32 | - | |
33 | - <TextView | |
34 | - android:id="@+id/textView1" | |
35 | - android:layout_width="wrap_content" | |
36 | - android:layout_height="wrap_content" | |
37 | - android:layout_alignParentLeft="true" | |
38 | - android:layout_alignParentTop="true" | |
39 | - android:text="Enable" | |
40 | - android:textAppearance="?android:attr/textAppearanceLarge" /> | |
41 | - | |
42 | - <EditText | |
43 | - android:id="@+id/text_interval" | |
44 | - android:layout_width="wrap_content" | |
45 | - android:layout_height="wrap_content" | |
46 | - android:layout_alignLeft="@+id/textView2" | |
47 | - android:layout_below="@+id/textView2" | |
48 | - android:ems="10" | |
49 | - android:inputType="numberDecimal" | |
50 | - android:text="30" > | |
51 | - | |
52 | - <requestFocus /> | |
53 | - </EditText> | |
54 | - | |
55 | - <TextView | |
56 | - android:id="@+id/textView3" | |
57 | - android:layout_width="wrap_content" | |
58 | - android:layout_height="wrap_content" | |
59 | - android:layout_alignBaseline="@+id/text_interval" | |
60 | - android:layout_alignBottom="@+id/text_interval" | |
61 | - android:layout_toRightOf="@+id/text_interval" | |
62 | - android:text="sec" /> | |
63 | - | |
64 | - <TextView | |
65 | - android:id="@+id/textView4" | |
66 | - android:layout_width="wrap_content" | |
67 | - android:layout_height="wrap_content" | |
68 | - android:layout_alignLeft="@+id/text_interval" | |
69 | - android:layout_below="@+id/text_interval" | |
70 | - android:layout_marginTop="16dp" | |
71 | - android:text="Trigger open URL" | |
72 | - android:textAppearance="?android:attr/textAppearanceLarge" /> | |
73 | - | |
74 | - <TextView | |
75 | - android:id="@+id/textView2" | |
76 | - android:layout_width="wrap_content" | |
77 | - android:layout_height="wrap_content" | |
78 | - android:layout_alignLeft="@+id/radioGroupEnabled" | |
79 | - android:layout_below="@+id/radioGroupEnabled" | |
80 | - android:layout_marginTop="16dp" | |
81 | - android:text="Watch Interval" | |
82 | - android:textAppearance="?android:attr/textAppearanceLarge" /> | |
83 | - | |
84 | - <EditText | |
85 | - android:id="@+id/text_open" | |
86 | - android:layout_width="wrap_content" | |
87 | - android:layout_height="wrap_content" | |
88 | - android:layout_alignLeft="@+id/textView4" | |
89 | - android:layout_alignParentRight="true" | |
90 | - android:layout_below="@+id/textView4" | |
91 | - android:ems="10" /> | |
11 | + <ScrollView | |
12 | + android:id="@+id/scrollView1" | |
13 | + android:layout_width="fill_parent" | |
14 | + android:layout_height="fill_parent" > | |
15 | + | |
16 | + <LinearLayout | |
17 | + android:layout_width="fill_parent" | |
18 | + android:layout_height="fill_parent" | |
19 | + android:orientation="vertical" | |
20 | + android:scrollbarStyle="insideOverlay" > | |
21 | + | |
22 | + <TextView | |
23 | + android:id="@+id/textView1" | |
24 | + android:layout_width="wrap_content" | |
25 | + android:layout_height="wrap_content" | |
26 | + android:text="Watcher status" | |
27 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
28 | + | |
29 | + <RadioGroup | |
30 | + android:id="@+id/radioGroupEnabled" | |
31 | + android:layout_width="wrap_content" | |
32 | + android:layout_height="wrap_content" | |
33 | + android:orientation="horizontal" > | |
34 | + | |
35 | + <RadioButton | |
36 | + android:id="@+id/radio_enabled" | |
37 | + android:layout_width="wrap_content" | |
38 | + android:layout_height="wrap_content" | |
39 | + android:checked="true" | |
40 | + android:text="Enabled" /> | |
41 | + | |
42 | + <RadioButton | |
43 | + android:id="@+id/radio_disabled" | |
44 | + android:layout_width="wrap_content" | |
45 | + android:layout_height="wrap_content" | |
46 | + android:text="Disabled" /> | |
47 | + </RadioGroup> | |
48 | + | |
49 | + <TextView | |
50 | + android:id="@+id/textView2" | |
51 | + android:layout_width="wrap_content" | |
52 | + android:layout_height="wrap_content" | |
53 | + android:layout_marginTop="16dp" | |
54 | + android:text="Watch interval" | |
55 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
56 | + | |
57 | + <LinearLayout | |
58 | + android:layout_width="wrap_content" | |
59 | + android:layout_height="wrap_content" > | |
60 | + | |
61 | + <EditText | |
62 | + android:id="@+id/text_interval" | |
63 | + android:layout_width="wrap_content" | |
64 | + android:layout_height="wrap_content" | |
65 | + android:layout_weight="1" | |
66 | + android:ems="10" | |
67 | + android:inputType="numberDecimal" | |
68 | + android:maxLines="1" | |
69 | + android:text="30" /> | |
70 | + | |
71 | + <TextView | |
72 | + android:id="@+id/textView3" | |
73 | + android:layout_width="wrap_content" | |
74 | + android:layout_height="wrap_content" | |
75 | + android:layout_weight="1" | |
76 | + android:text="sec" /> | |
77 | + </LinearLayout> | |
78 | + | |
79 | + <TextView | |
80 | + android:id="@+id/textView6" | |
81 | + android:layout_width="wrap_content" | |
82 | + android:layout_height="wrap_content" | |
83 | + android:layout_marginTop="16dp" | |
84 | + android:text="Watch target package" | |
85 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
86 | + | |
87 | + <EditText | |
88 | + android:id="@+id/text_watch" | |
89 | + android:layout_width="fill_parent" | |
90 | + android:layout_height="wrap_content" | |
91 | + android:ems="10" | |
92 | + android:minLines="1" /> | |
93 | + | |
94 | + <TextView | |
95 | + android:id="@+id/textView5" | |
96 | + android:layout_width="wrap_content" | |
97 | + android:layout_height="wrap_content" | |
98 | + android:layout_marginTop="18dp" | |
99 | + android:text="Launch method" | |
100 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
101 | + | |
102 | + <RadioGroup | |
103 | + android:id="@+id/radioGroupMethod" | |
104 | + android:layout_width="wrap_content" | |
105 | + android:layout_height="wrap_content" | |
106 | + android:orientation="horizontal" > | |
107 | + | |
108 | + <RadioButton | |
109 | + android:id="@+id/radio_launch_package" | |
110 | + android:layout_width="wrap_content" | |
111 | + android:layout_height="wrap_content" | |
112 | + android:checked="true" | |
113 | + android:text="Run package" /> | |
114 | + | |
115 | + <RadioButton | |
116 | + android:id="@+id/radio_launch_url" | |
117 | + android:layout_width="wrap_content" | |
118 | + android:layout_height="wrap_content" | |
119 | + android:text="Open URL" /> | |
120 | + </RadioGroup> | |
121 | + | |
122 | + <TextView | |
123 | + android:id="@+id/textView4" | |
124 | + android:layout_width="wrap_content" | |
125 | + android:layout_height="wrap_content" | |
126 | + android:layout_marginTop="16dp" | |
127 | + android:text="Trigger open URL" | |
128 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
129 | + | |
130 | + <EditText | |
131 | + android:id="@+id/text_open" | |
132 | + android:layout_width="fill_parent" | |
133 | + android:layout_height="wrap_content" | |
134 | + android:ems="10" | |
135 | + android:minLines="1" > | |
136 | + | |
137 | + <requestFocus /> | |
138 | + </EditText> | |
139 | + | |
140 | + <TextView | |
141 | + android:id="@+id/textView7" | |
142 | + android:layout_width="wrap_content" | |
143 | + android:layout_height="wrap_content" | |
144 | + android:text="Event handlers" | |
145 | + android:textAppearance="?android:attr/textAppearanceLarge" /> | |
146 | + | |
147 | + <CheckBox | |
148 | + android:id="@+id/check_boot" | |
149 | + android:layout_width="wrap_content" | |
150 | + android:layout_height="wrap_content" | |
151 | + android:text="Enable on boot" /> | |
152 | + | |
153 | + <CheckBox | |
154 | + android:id="@+id/check_power_connected" | |
155 | + android:layout_width="wrap_content" | |
156 | + android:layout_height="wrap_content" | |
157 | + android:text="Enable on power connected" /> | |
158 | + | |
159 | + <CheckBox | |
160 | + android:id="@+id/check_power_disconnected" | |
161 | + android:layout_width="wrap_content" | |
162 | + android:layout_height="wrap_content" | |
163 | + android:text="Disable on power disconnected" /> | |
164 | + </LinearLayout> | |
165 | + | |
166 | + </ScrollView> | |
92 | 167 | |
93 | 168 | </RelativeLayout> |
\ No newline at end of file |
@@ -0,0 +1,27 @@ | ||
1 | +package jp.nekoteki.android.demowatcher; | |
2 | + | |
3 | +import android.content.BroadcastReceiver; | |
4 | +import android.content.Context; | |
5 | +import android.content.Intent; | |
6 | +import android.content.SharedPreferences; | |
7 | +import android.preference.PreferenceManager; | |
8 | + | |
9 | +public class EventReceiver extends BroadcastReceiver { | |
10 | + | |
11 | + @Override | |
12 | + public void onReceive(Context context, Intent intent) { | |
13 | + String act = intent.getAction(); | |
14 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | |
15 | + if (act.equals(Intent.ACTION_BOOT_COMPLETED) && | |
16 | + prefs.getBoolean("handle_boot", true)) { | |
17 | + new WatchService().schedule(context); | |
18 | + } else if (act.equals(Intent.ACTION_POWER_CONNECTED) && | |
19 | + prefs.getBoolean("handle_power_connected", true)) { | |
20 | + new WatchService().schedule(context); | |
21 | + } else if (act.equals(Intent.ACTION_POWER_DISCONNECTED) && | |
22 | + prefs.getBoolean("handle_power_disconnected", true)) { | |
23 | + new WatchService().cancelSchedule(context); | |
24 | + } | |
25 | + | |
26 | + } | |
27 | +} |
@@ -1,5 +1,6 @@ | ||
1 | 1 | package jp.nekoteki.android.demowatcher; |
2 | 2 | |
3 | +import android.R.bool; | |
3 | 4 | import android.os.Bundle; |
4 | 5 | import android.os.Environment; |
5 | 6 | import android.preference.PreferenceManager; |
@@ -15,6 +16,9 @@ import android.text.Editable; | ||
15 | 16 | import android.text.TextWatcher; |
16 | 17 | import android.util.Log; |
17 | 18 | import android.view.Menu; |
19 | +import android.view.View; | |
20 | +import android.widget.CheckBox; | |
21 | +import android.widget.CompoundButton; | |
18 | 22 | import android.widget.EditText; |
19 | 23 | import android.widget.RadioButton; |
20 | 24 | import android.widget.RadioGroup; |
@@ -34,10 +38,8 @@ public class MainActivity extends Activity { | ||
34 | 38 | |
35 | 39 | RadioGroup enable_raidos = (RadioGroup) findViewById(R.id.radioGroupEnabled); |
36 | 40 | enable_raidos.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
37 | - | |
38 | 41 | @Override |
39 | 42 | public void onCheckedChanged(RadioGroup group, int checkedId) { |
40 | - // TODO Auto-generated method stub | |
41 | 43 | if (checkedId == R.id.radio_disabled) { |
42 | 44 | MainActivity.this.cancelSchedule(); |
43 | 45 | } else { |
@@ -47,62 +49,125 @@ public class MainActivity extends Activity { | ||
47 | 49 | } |
48 | 50 | }); |
49 | 51 | |
52 | + if (prefs.getString("method", "package").equals("url")) { | |
53 | + ((RadioButton) findViewById(R.id.radio_launch_url)).setChecked(true); | |
54 | + } else { | |
55 | + ((RadioButton) findViewById(R.id.radio_launch_package)).setChecked(true);; | |
56 | + } | |
57 | + RadioGroup method_raidos = (RadioGroup) findViewById(R.id.radioGroupMethod); | |
58 | + method_raidos.setOnCheckedChangeListener(new OnCheckedChangeListener() { | |
59 | + @Override | |
60 | + public void onCheckedChanged(RadioGroup group, int checkedId) { | |
61 | + Editor peditor = MainActivity.this.prefs.edit(); | |
62 | + if (checkedId == R.id.radio_launch_url) { | |
63 | + peditor.putString("method", "url"); | |
64 | + } else { | |
65 | + peditor.putString("method", "package"); | |
66 | + } | |
67 | + peditor.commit(); | |
68 | + MainActivity.this.reSchdule(); | |
69 | + } | |
70 | + }); | |
71 | + | |
50 | 72 | EditText interval = (EditText) findViewById(R.id.text_interval); |
51 | 73 | interval.setText(String.valueOf(prefs.getInt("interval", WatchService.DEFAULT_INTERVAL))); |
52 | 74 | interval.addTextChangedListener(new TextWatcher() { |
53 | - | |
54 | 75 | @Override |
55 | - public void onTextChanged(CharSequence s, int start, int before, int count) { | |
56 | - return; | |
57 | - } | |
76 | + public void onTextChanged(CharSequence s, int start, int before, int count) {} | |
58 | 77 | |
59 | 78 | @Override |
60 | - public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
61 | - return; | |
62 | - } | |
79 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
63 | 80 | |
64 | 81 | @Override |
65 | 82 | public void afterTextChanged(Editable s) { |
66 | 83 | Editor peditor = MainActivity.this.prefs.edit(); |
67 | 84 | peditor.putInt("interval", MainActivity.this.getInterval()/1000); |
68 | 85 | peditor.commit(); |
86 | + MainActivity.this.reSchdule(); | |
69 | 87 | } |
70 | 88 | }); |
71 | 89 | |
72 | 90 | EditText open = (EditText) findViewById(R.id.text_open); |
73 | 91 | open.setText(prefs.getString("open", WatchService.DEFAULT_OPEN)); |
74 | 92 | open.addTextChangedListener(new TextWatcher() { |
93 | + @Override | |
94 | + public void onTextChanged(CharSequence s, int start, int before, int count) {} | |
75 | 95 | |
76 | 96 | @Override |
77 | - public void onTextChanged(CharSequence s, int start, int before, int count) { | |
78 | - return; | |
97 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
98 | + | |
99 | + @Override | |
100 | + public void afterTextChanged(Editable s) { | |
101 | + Editor peditor = MainActivity.this.prefs.edit(); | |
102 | + peditor.putString("open", s.toString()); | |
103 | + peditor.commit(); | |
104 | + MainActivity.this.reSchdule(); | |
79 | 105 | } |
106 | + }); | |
107 | + | |
108 | + EditText watch = (EditText) findViewById(R.id.text_watch); | |
109 | + watch.setText(prefs.getString("watch", WatchService.DEFAULT_WATCH)); | |
110 | + watch.addTextChangedListener(new TextWatcher() { | |
111 | + @Override | |
112 | + public void onTextChanged(CharSequence s, int start, int before, int count) {} | |
113 | + | |
114 | + @Override | |
115 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
80 | 116 | |
81 | 117 | @Override |
82 | - public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
83 | - return; | |
84 | - | |
118 | + public void afterTextChanged(Editable s) { | |
119 | + Editor peditor = MainActivity.this.prefs.edit(); | |
120 | + peditor.putString("watch", s.toString()); | |
121 | + peditor.commit(); | |
122 | + MainActivity.this.reSchdule(); | |
123 | + } | |
124 | + }); | |
125 | + | |
126 | + CheckBox check_boot = (CheckBox) findViewById(R.id.check_boot); | |
127 | + check_boot.setChecked(prefs.getBoolean("handle_boot", true)); | |
128 | + check_boot.setOnClickListener(new View.OnClickListener() { | |
129 | + @Override | |
130 | + public void onClick(View v) { | |
131 | + Editor peditor = MainActivity.this.prefs.edit(); | |
132 | + peditor.putBoolean("handle_boot", ((CheckBox) v).isChecked()); | |
133 | + peditor.commit(); | |
85 | 134 | } |
135 | + }); | |
86 | 136 | |
137 | + CheckBox check_pc = (CheckBox) findViewById(R.id.check_power_connected); | |
138 | + check_pc.setChecked(prefs.getBoolean("handle_boot", true)); | |
139 | + check_pc.setOnClickListener(new View.OnClickListener() { | |
87 | 140 | @Override |
88 | - public void afterTextChanged(Editable s) { | |
141 | + public void onClick(View v) { | |
89 | 142 | Editor peditor = MainActivity.this.prefs.edit(); |
90 | - peditor.putString("open", s.toString()); | |
143 | + peditor.putBoolean("handle_power_connected", ((CheckBox) v).isChecked()); | |
144 | + peditor.commit(); | |
145 | + } | |
146 | + }); | |
147 | + | |
148 | + CheckBox check_pd = (CheckBox) findViewById(R.id.check_power_disconnected); | |
149 | + check_pd.setChecked(prefs.getBoolean("handle_boot", true)); | |
150 | + check_pd.setOnClickListener(new View.OnClickListener() { | |
151 | + @Override | |
152 | + public void onClick(View v) { | |
153 | + Editor peditor = MainActivity.this.prefs.edit(); | |
154 | + peditor.putBoolean("handle_power_disconnected", ((CheckBox) v).isChecked()); | |
91 | 155 | peditor.commit(); |
92 | 156 | } |
93 | 157 | }); |
158 | + | |
159 | + if (prefs.getBoolean("enabled", false)) | |
160 | + schedule(); | |
94 | 161 | } |
95 | 162 | |
96 | 163 | @Override |
97 | 164 | protected void onResume() { |
98 | 165 | super.onResume(); |
99 | 166 | |
100 | - RadioButton radio_enabled = (RadioButton) findViewById(R.id.radio_enabled); | |
101 | - RadioButton radio_disabled = (RadioButton) findViewById(R.id.radio_disabled); | |
102 | 167 | if (this.prefs.getBoolean("enabled", false)) { |
103 | - radio_enabled.setChecked(true); | |
168 | + ((RadioButton) findViewById(R.id.radio_enabled)).setChecked(true);; | |
104 | 169 | } else { |
105 | - radio_disabled.setChecked(true); | |
170 | + ((RadioButton) findViewById(R.id.radio_disabled)).setChecked(true); | |
106 | 171 | } |
107 | 172 | } |
108 | 173 |
@@ -113,16 +178,21 @@ public class MainActivity extends Activity { | ||
113 | 178 | return true; |
114 | 179 | } |
115 | 180 | |
116 | - public void cancelSchedule() { | |
117 | - watcher.cancelSchedule(getApplicationContext()); | |
181 | + public void reSchdule() { | |
182 | + if (cancelSchedule()) | |
183 | + schedule(); | |
184 | + } | |
185 | + | |
186 | + public boolean cancelSchedule() { | |
187 | + return watcher.cancelSchedule(getApplicationContext()); | |
118 | 188 | } |
119 | 189 | |
120 | - public void schedule() { | |
190 | + public boolean schedule() { | |
121 | 191 | Log.d(this.getClass().getSimpleName(), "Enable watch schedule"); |
122 | 192 | Editor peditor = this.prefs.edit(); |
123 | 193 | peditor.putBoolean("enabled", true); |
124 | 194 | peditor.commit(); |
125 | - watcher.schedule(getApplicationContext()); | |
195 | + return watcher.schedule(getApplicationContext()); | |
126 | 196 | } |
127 | 197 | |
128 | 198 | public int getInterval() { |
@@ -1,14 +0,0 @@ | ||
1 | -package jp.nekoteki.android.demowatcher; | |
2 | - | |
3 | -import android.content.BroadcastReceiver; | |
4 | -import android.content.Context; | |
5 | -import android.content.Intent; | |
6 | -import android.widget.Toast; | |
7 | - | |
8 | -public class StartReceiver extends BroadcastReceiver { | |
9 | - | |
10 | - @Override | |
11 | - public void onReceive(Context context, Intent intent) { | |
12 | - new WatchService().schedule(context); | |
13 | - } | |
14 | -} |
@@ -1,14 +0,0 @@ | ||
1 | -package jp.nekoteki.android.demowatcher; | |
2 | - | |
3 | -import android.content.BroadcastReceiver; | |
4 | -import android.content.Context; | |
5 | -import android.content.Intent; | |
6 | -import android.widget.Toast; | |
7 | - | |
8 | -public class StopReceiver extends BroadcastReceiver { | |
9 | - | |
10 | - @Override | |
11 | - public void onReceive(Context context, Intent intent) { | |
12 | - new WatchService().cancelSchedule(context); | |
13 | - } | |
14 | -} |
@@ -13,6 +13,7 @@ import android.content.Intent; | ||
13 | 13 | import android.content.SharedPreferences; |
14 | 14 | import android.content.SharedPreferences.Editor; |
15 | 15 | import android.net.Uri; |
16 | +import android.os.Handler; | |
16 | 17 | import android.os.PowerManager; |
17 | 18 | import android.preference.PreferenceManager; |
18 | 19 | import android.provider.BaseColumns; |
@@ -27,13 +28,16 @@ public class WatchService extends IntentService { | ||
27 | 28 | final public static String DEFAULT_WATCH = "com.mxtech.videoplayer.pro"; |
28 | 29 | final public static String DEFAULT_OPEN = "file:///sdcard/test/test.mp4"; |
29 | 30 | final public static int DEFAULT_INTERVAL = 30; |
31 | + public Handler handler; | |
30 | 32 | |
31 | 33 | public WatchService() { |
32 | 34 | super(NAME); |
35 | + this.handler = new Handler(); | |
33 | 36 | } |
34 | 37 | |
35 | 38 | public WatchService(String name) { |
36 | 39 | super(name); |
40 | + this.handler = new Handler(); | |
37 | 41 | } |
38 | 42 | |
39 | 43 | public PendingIntent getPendingIntent(Context context) { |
@@ -50,6 +54,7 @@ public class WatchService extends IntentService { | ||
50 | 54 | Intent intent = new Intent(context, WatchService.class); |
51 | 55 | intent.putExtra("open", this.getOpenTarget(context)); |
52 | 56 | intent.putExtra("watch", this.getWatchTarget(context)); |
57 | + intent.putExtra("method", this.getLaunchMethod(context)); | |
53 | 58 | return PendingIntent.getService(context, 0, intent, flag); |
54 | 59 | } |
55 | 60 |
@@ -65,36 +70,43 @@ public class WatchService extends IntentService { | ||
65 | 70 | return getPrefs(c).getString("open", DEFAULT_OPEN); |
66 | 71 | } |
67 | 72 | |
73 | + public String getLaunchMethod(Context c) { | |
74 | + return getPrefs(c).getString("method", "package"); | |
75 | + } | |
76 | + | |
68 | 77 | public int getInterval(Context c) { |
69 | 78 | return getPrefs(c).getInt("interval", DEFAULT_INTERVAL); |
70 | 79 | } |
71 | 80 | |
72 | 81 | |
73 | - public void cancelSchedule(Context context) { | |
82 | + public boolean cancelSchedule(Context context) { | |
74 | 83 | Editor peditor = getPrefs(context).edit(); |
75 | 84 | peditor.putBoolean("enabled", false); |
76 | 85 | peditor.commit(); |
77 | 86 | |
78 | 87 | PendingIntent intent = this.getPendingIntent(context); |
79 | - if (intent == null) return; | |
88 | + if (intent == null) return false; | |
80 | 89 | Log.d(this.getClass().getSimpleName(), "Disable watch schedule"); |
81 | 90 | AlarmManager manager = (AlarmManager) context.getSystemService(ALARM_SERVICE); |
82 | 91 | manager.cancel(intent); |
83 | 92 | intent.cancel(); |
84 | 93 | |
85 | 94 | killWatchTarget(context); |
95 | + return true; | |
86 | 96 | } |
87 | 97 | |
88 | - public void schedule(Context context) { | |
98 | + public boolean schedule(Context context) { | |
89 | 99 | Editor peditor = getPrefs(context).edit(); |
90 | 100 | peditor.putBoolean("enabled", true); |
91 | 101 | peditor.commit(); |
92 | 102 | |
103 | + if (this.getPendingIntent(context) != null) | |
104 | + return false; | |
93 | 105 | AlarmManager manager = (AlarmManager) context.getSystemService(ALARM_SERVICE); |
94 | 106 | manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, |
95 | 107 | System.currentTimeMillis(), this.getInterval(context) * 1000, |
96 | 108 | this.getPendingIntent(context, true)); |
97 | - | |
109 | + return true; | |
98 | 110 | } |
99 | 111 | |
100 | 112 | @Override |
@@ -102,24 +114,53 @@ public class WatchService extends IntentService { | ||
102 | 114 | Log.d(NAME, "Run watch handle!"); |
103 | 115 | |
104 | 116 | this.forceWake(); |
117 | + // TODO: unlock support. | |
105 | 118 | |
106 | 119 | if (checkTopActivity(intent.getStringExtra("watch"))) |
107 | 120 | return; |
121 | + | |
122 | + try { | |
123 | + if (intent.getStringExtra("method").equals("url")) { | |
124 | + launchWithURL(intent); | |
125 | + } else { | |
126 | + launchWithPackage(intent); | |
127 | + } | |
128 | + } catch (Exception e) { | |
129 | + Log.d(NAME, "Faild to Start: "+e.getMessage()); | |
130 | + handler.post(new Runnable() { | |
131 | + public String message = ""; | |
132 | + public Runnable setMessage(String message) { | |
133 | + this.message = message; | |
134 | + return this; | |
135 | + } | |
136 | + @Override | |
137 | + public void run() { | |
138 | + Toast.makeText(getApplicationContext(), | |
139 | + "[DemoWatcher] Failed to start target!\n"+this.message, | |
140 | + Toast.LENGTH_LONG).show(); | |
141 | + } | |
142 | + }.setMessage(e.getMessage())); | |
143 | + } | |
144 | + } | |
145 | + | |
146 | + protected void launchWithPackage(Intent intent) { | |
147 | + String watch = intent.getStringExtra("watch"); | |
148 | + Intent openintent = getPackageManager().getLaunchIntentForPackage(watch); | |
149 | + openintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); | |
150 | + | |
151 | + Log.d(NAME, "Sending package "+watch); | |
108 | 152 | |
153 | + startActivity(openintent); | |
154 | + } | |
155 | + | |
156 | + protected void launchWithURL(Intent intent) { | |
109 | 157 | String url = intent.getStringExtra("open"); |
110 | 158 | Intent openintent = new Intent(Intent.ACTION_VIEW); |
111 | 159 | openintent.setData(Uri.parse(url)); |
112 | 160 | openintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
113 | 161 | |
114 | 162 | Log.d(NAME, "Sending view intent with "+url); |
115 | - | |
116 | - try { | |
117 | - startActivity(openintent); | |
118 | - } catch (Exception e) { | |
119 | - Log.d(NAME, "Faild to Start: "+e.getMessage()); | |
120 | - } | |
121 | - | |
122 | - // TODO: unlock support. | |
163 | + startActivity(openintent); | |
123 | 164 | } |
124 | 165 | |
125 | 166 | public void killWatchTarget(Context context) { |