1 package de
.ub0r
.android
.adBlock
;
3 import java
.util
.ArrayList
;
5 import android
.app
.Activity
;
6 import android
.app
.AlertDialog
;
7 import android
.content
.DialogInterface
;
8 import android
.content
.Intent
;
9 import android
.content
.SharedPreferences
;
10 import android
.os
.Bundle
;
11 import android
.preference
.PreferenceManager
;
12 import android
.view
.View
;
13 import android
.view
.View
.OnClickListener
;
14 import android
.widget
.AdapterView
;
15 import android
.widget
.ArrayAdapter
;
16 import android
.widget
.Button
;
17 import android
.widget
.EditText
;
18 import android
.widget
.ListView
;
19 import android
.widget
.AdapterView
.OnItemClickListener
;
22 * Main Activity to controle ad blocking proxy.
26 public class AdBlock
extends Activity
implements OnClickListener
,
29 /** Preferences: Port. */
30 static final String PREFS_PORT
= "port";
31 /** Preferences: Filter. */
32 static final String PREFS_FILTER
= "filter";
34 /** ItemDialog: edit. */
35 private static final short ITEM_DIALOG_EDIT
= 0;
36 /** ItemDialog: delete. */
37 private static final short ITEM_DIALOG_DELETE
= 1;
40 private SharedPreferences preferences
;
43 private ArrayList
<String
> filter
= new ArrayList
<String
>();
44 /** The ArrayAdapter. */
45 private ArrayAdapter
<String
> adapter
= null;
48 private int itemToEdit
= -1;
51 * Called when the activity is first created.
53 * @param savedInstanceState
57 public final void onCreate(final Bundle savedInstanceState
) {
58 super.onCreate(savedInstanceState
);
59 this.setContentView(R
.layout
.main
);
61 this.preferences
= PreferenceManager
.getDefaultSharedPreferences(this);
62 ((EditText
) this.findViewById(R
.id
.port
)).setText(this.preferences
63 .getString(PREFS_PORT
, "8080"));
64 String f
= this.preferences
.getString(PREFS_FILTER
, "");
65 for (String s
: f
.split("\n")) {
71 ((Button
) this.findViewById(R
.id
.start_service
))
72 .setOnClickListener(this);
73 ((Button
) this.findViewById(R
.id
.stop_service
))
74 .setOnClickListener(this);
75 ((Button
) this.findViewById(R
.id
.filter_add_
)).setOnClickListener(this);
76 ListView lv
= (ListView
) this.findViewById(R
.id
.filter
);
77 this.adapter
= new ArrayAdapter
<String
>(this,
78 android
.R
.layout
.simple_list_item_1
, this.filter
);
79 lv
.setAdapter(this.adapter
);
80 lv
.setTextFilterEnabled(true);
81 lv
.setOnItemClickListener(this);
84 /** Save Preferences. */
85 private void savePreferences() {
86 SharedPreferences
.Editor editor
= this.preferences
.edit();
87 editor
.putString(PREFS_PORT
, ((EditText
) this.findViewById(R
.id
.port
))
88 .getText().toString());
89 StringBuilder sb
= new StringBuilder();
90 for (String s
: this.filter
) {
93 editor
.putString(PREFS_FILTER
, sb
.toString());
97 /** Called on pause. */
99 public final void onPause() {
101 this.savePreferences();
111 public final void onClick(final View v
) {
113 case R
.id
.start_service
:
114 this.savePreferences();
115 this.startService(new Intent(this, Proxy
.class));
117 case R
.id
.stop_service
:
118 this.stopService(new Intent(this, Proxy
.class));
119 case R
.id
.filter_add_
:
120 EditText et
= (EditText
) this.findViewById(R
.id
.filter_add
);
121 String f
= et
.getText().toString();
122 if (f
.length() > 0) {
123 if (this.itemToEdit
>= 0) {
124 this.filter
.remove(this.itemToEdit
);
125 this.itemToEdit
= -1;
129 this.adapter
.notifyDataSetChanged();
138 * Handle clicked ListItem.
150 public final void onItemClick(final AdapterView
<?
> parent
, final View v
,
151 final int position
, final long id
) {
153 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
154 // builder.setTitle("Pick a color");
156 this.getResources().getStringArray(R
.array
.itemDialog
),
157 new DialogInterface
.OnClickListener() {
158 public void onClick(final DialogInterface dialog
,
161 case ITEM_DIALOG_EDIT
:
162 AdBlock
.this.itemToEdit
= position
;
163 ((EditText
) AdBlock
.this
164 .findViewById(R
.id
.filter_add
))
165 .setText(AdBlock
.this.adapter
168 case ITEM_DIALOG_DELETE
:
169 AdBlock
.this.filter
.remove(position
);
170 AdBlock
.this.adapter
.notifyDataSetChanged();
177 AlertDialog alert
= builder
.create();