Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/ServiceBinder.java

Issue 4705284891082752: Proxy configurators (Closed)
Patch Set: Created Aug. 11, 2014, 12:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.android;
19
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.IBinder;
25
26 public class ServiceBinder
27 {
28 private final Context context;
29 private volatile ProxyService proxyService;
30 private final ProxyServiceConnection proxyServiceConnection;
31 private OnConnectHandler onConnectHandler = null;
32
33 public ServiceBinder(final Context context)
34 {
35 this.context = context;
36 this.proxyServiceConnection = new ProxyServiceConnection(this);
37 }
38
39 public ServiceBinder setOnConnectHandler(final OnConnectHandler handler)
40 {
41 this.onConnectHandler = handler;
42 return this;
43 }
44
45 public synchronized void bind()
46 {
47 if (this.proxyService == null)
48 {
49 this.context.bindService(new Intent(this.context, ProxyService.class), thi s.proxyServiceConnection, 0);
50 }
51 }
52
53 public ProxyService get()
54 {
55 return this.proxyService;
56 }
57
58 public void unbind()
59 {
60 this.context.unbindService(this.proxyServiceConnection);
61 this.proxyService = null;
62 }
63
64 private static class ProxyServiceConnection implements ServiceConnection
65 {
66 private final ServiceBinder binder;
67
68 private ProxyServiceConnection(final ServiceBinder binder)
69 {
70 this.binder = binder;
71 }
72
73 @Override
74 public void onServiceConnected(final ComponentName name, final IBinder servi ce)
75 {
76 final ProxyService proxyService = ((ProxyService.LocalBinder) service).get Service();
77 final OnConnectHandler handler = this.binder.onConnectHandler;
78
79 this.binder.proxyService = proxyService;
80
81 if (handler != null)
82 {
83 handler.onConnect(proxyService);
84 }
85 }
86
87 @Override
88 public void onServiceDisconnected(final ComponentName name)
89 {
90 this.binder.proxyService = null;
91 }
92 }
93
94 public interface OnConnectHandler
95 {
96 public void onConnect(final ProxyService proxyService);
97 }
98 }
OLDNEW

Powered by Google App Engine
This is Rietveld