OLD | NEW |
(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.configurators; |
| 19 |
| 20 import java.net.InetAddress; |
| 21 |
| 22 import org.adblockplus.android.Utils; |
| 23 import org.adblockplus.android.compat.CmGlobalProxyManager; |
| 24 import org.adblockplus.android.compat.ProxyProperties; |
| 25 |
| 26 import android.content.BroadcastReceiver; |
| 27 import android.content.Context; |
| 28 import android.content.Intent; |
| 29 import android.content.IntentFilter; |
| 30 import android.util.Log; |
| 31 |
| 32 public class CyanogenProxyConfigurator implements ProxyConfigurator |
| 33 { |
| 34 private static final String TAG = Utils.getTag(CyanogenProxyConfigurator.class
); |
| 35 |
| 36 private final Context context; |
| 37 private ProxyProperties proxyProperties = null; |
| 38 private final GlobalProxyReceiver globalProxyReceiver; |
| 39 private volatile boolean isRegistered = false; |
| 40 |
| 41 public CyanogenProxyConfigurator(final Context context) |
| 42 { |
| 43 this.context = context; |
| 44 this.globalProxyReceiver = new GlobalProxyReceiver(this); |
| 45 } |
| 46 |
| 47 @Override |
| 48 public boolean initialize() |
| 49 { |
| 50 try |
| 51 { |
| 52 final boolean success = new ProxyProperties("localhost", 8080, "").toCmPro
xyProperties() != null; |
| 53 |
| 54 if (success) |
| 55 { |
| 56 this.context.registerReceiver(this.globalProxyReceiver, new IntentFilter
(CmGlobalProxyManager.GLOBAL_PROXY_STATE_CHANGE_ACTION)); |
| 57 } |
| 58 |
| 59 return success; |
| 60 } |
| 61 catch (final Throwable t) |
| 62 { |
| 63 return false; |
| 64 } |
| 65 } |
| 66 |
| 67 @Override |
| 68 public boolean registerProxy(final InetAddress address, final int port) |
| 69 { |
| 70 try |
| 71 { |
| 72 final CmGlobalProxyManager globalProxyManager = new CmGlobalProxyManager(t
his.context); |
| 73 globalProxyManager.setGlobalProxy(this.proxyProperties = new ProxyProperti
es(address.getHostName(), port)); |
| 74 return this.isRegistered = true; |
| 75 } |
| 76 catch (final Throwable t) |
| 77 { |
| 78 Log.d(TAG, "Failed to register proxy using CMapi", t); |
| 79 return false; |
| 80 } |
| 81 } |
| 82 |
| 83 @Override |
| 84 public void unregisterProxy() |
| 85 { |
| 86 // TODO Do we need to do something here? |
| 87 } |
| 88 |
| 89 @Override |
| 90 public void shutdown() |
| 91 { |
| 92 this.context.unregisterReceiver(this.globalProxyReceiver); |
| 93 } |
| 94 |
| 95 @Override |
| 96 public ProxyRegistrationType getType() |
| 97 { |
| 98 return ProxyRegistrationType.CYANOGENMOD; |
| 99 } |
| 100 |
| 101 @Override |
| 102 public boolean isRegistered() |
| 103 { |
| 104 return this.isRegistered; |
| 105 } |
| 106 |
| 107 @Override |
| 108 public boolean isSticky() |
| 109 { |
| 110 return true; |
| 111 } |
| 112 |
| 113 @Override |
| 114 public String toString() |
| 115 { |
| 116 return "[ProxyConfigurator: " + this.getType() + "]"; |
| 117 } |
| 118 |
| 119 private static class GlobalProxyReceiver extends BroadcastReceiver |
| 120 { |
| 121 private final CyanogenProxyConfigurator cyanogenProxyConfigurator; |
| 122 |
| 123 public GlobalProxyReceiver(final CyanogenProxyConfigurator cyanogenProxyConf
igurator) |
| 124 { |
| 125 this.cyanogenProxyConfigurator = cyanogenProxyConfigurator; |
| 126 } |
| 127 |
| 128 @Override |
| 129 public void onReceive(final Context context, final Intent intent) |
| 130 { |
| 131 if (intent != null && CmGlobalProxyManager.GLOBAL_PROXY_STATE_CHANGE_ACTIO
N.equals(intent.getAction())) |
| 132 { |
| 133 try |
| 134 { |
| 135 final CmGlobalProxyManager globalProxyManager = new CmGlobalProxyManag
er(this.cyanogenProxyConfigurator.context); |
| 136 globalProxyManager.setGlobalProxy(this.cyanogenProxyConfigurator.proxy
Properties); |
| 137 } |
| 138 catch (final Throwable t) |
| 139 { |
| 140 Log.d(TAG, "GlobalProxyReceiver failed to register proxy", t); |
| 141 } |
| 142 } |
| 143 } |
| 144 } |
| 145 } |
OLD | NEW |