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 private static final String PREFS_PORT
= "port";
31 /** Preferences: Filter. */
32 private 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 /** Called on pause. */
86 public final void onPause() {
88 SharedPreferences
.Editor editor
= this.preferences
.edit();
89 editor
.putString(PREFS_PORT
, ((EditText
) this.findViewById(R
.id
.port
))
90 .getText().toString());
91 StringBuilder sb
= new StringBuilder();
92 for (String s
: this.filter
) {
95 editor
.putString(PREFS_FILTER
, sb
.toString());
106 public final void onClick(final View v
) {
108 case R
.id
.start_service
:
109 this.startService(new Intent(this, Proxy
.class));
111 case R
.id
.stop_service
:
112 this.stopService(new Intent(this, Proxy
.class));
113 case R
.id
.filter_add_
:
114 EditText et
= (EditText
) this.findViewById(R
.id
.filter_add
);
115 String f
= et
.getText().toString();
116 if (f
.length() > 0) {
117 if (this.itemToEdit
>= 0) {
118 this.filter
.remove(this.itemToEdit
);
119 this.itemToEdit
= -1;
123 this.adapter
.notifyDataSetChanged();
132 * Handle clicked ListItem.
144 public final void onItemClick(final AdapterView
<?
> parent
, final View v
,
145 final int position
, final long id
) {
147 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
148 // builder.setTitle("Pick a color");
150 this.getResources().getStringArray(R
.array
.itemDialog
),
151 new DialogInterface
.OnClickListener() {
152 public void onClick(final DialogInterface dialog
,
155 case ITEM_DIALOG_EDIT
:
156 AdBlock
.this.itemToEdit
= position
;
157 ((EditText
) AdBlock
.this
158 .findViewById(R
.id
.filter_add
))
159 .setText(AdBlock
.this.adapter
162 case ITEM_DIALOG_DELETE
:
163 AdBlock
.this.filter
.remove(position
);
164 AdBlock
.this.adapter
.notifyDataSetChanged();
171 AlertDialog alert
= builder
.create();