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.lang.reflect.Constructor; |
| 21 import java.net.InetAddress; |
| 22 import java.util.ArrayList; |
| 23 import java.util.HashMap; |
| 24 |
| 25 import org.adblockplus.android.Utils; |
| 26 |
| 27 import android.content.Context; |
| 28 import android.util.Log; |
| 29 |
| 30 /** |
| 31 * Common proxy configurator methods. |
| 32 */ |
| 33 public final class ProxyConfigurators |
| 34 { |
| 35 private static final String TAG = Utils.getTag(ProxyConfigurators.class); |
| 36 private static final ArrayList<Class<?>> CONFIGURATORS = new ArrayList<Class<?
>>(); |
| 37 private static final HashMap<ProxyRegistrationType, Class<?>> CONFIGURATOR_MAP
= new HashMap<ProxyRegistrationType, Class<?>>(); |
| 38 private static final HashMap<Class<?>, ProxyRegistrationType> CONFIGURATOR_INV
_MAP = new HashMap<Class<?>, ProxyRegistrationType>(); |
| 39 |
| 40 static |
| 41 { |
| 42 /* |
| 43 * Add new proxy-configurators here. |
| 44 * |
| 45 * Note: order of adding is important (as later added configurators act as f
allbacks for earlier ones). |
| 46 */ |
| 47 add(CyanogenProxyConfigurator.class); |
| 48 add(IptablesProxyConfigurator.class); |
| 49 add(NativeProxyConfigurator.class); |
| 50 add(ManualProxyConfigurator.class); |
| 51 } |
| 52 |
| 53 private ProxyConfigurators() |
| 54 { |
| 55 // no instantiation |
| 56 } |
| 57 |
| 58 private static void add(final Class<?> clazz) |
| 59 { |
| 60 try |
| 61 { |
| 62 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 63 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance((Context
) null); |
| 64 final ProxyRegistrationType type = pc.getType(); |
| 65 |
| 66 if (!CONFIGURATOR_MAP.containsKey(type)) |
| 67 { |
| 68 CONFIGURATORS.add(clazz); |
| 69 CONFIGURATOR_MAP.put(type, clazz); |
| 70 CONFIGURATOR_INV_MAP.put(clazz, type); |
| 71 } |
| 72 else |
| 73 { |
| 74 // We fail hard here. |
| 75 throw new IllegalArgumentException("Duplicate proxy configurator (" + cl
azz + ") for type: " + type); |
| 76 } |
| 77 } |
| 78 catch (final Exception e) |
| 79 { |
| 80 // We fail hard here. |
| 81 throw new IllegalStateException("Failed to register proxy configurator ("
+ clazz + ")", e); |
| 82 } |
| 83 } |
| 84 |
| 85 private static ProxyConfigurator initialize(final Context context, final Proxy
Configurator from) |
| 86 { |
| 87 int i = 0; |
| 88 |
| 89 if (from != null) |
| 90 { |
| 91 // Scan through configurators to find last used one |
| 92 while (i < CONFIGURATORS.size() && !CONFIGURATORS.get(i).equals(from.getCl
ass())) |
| 93 { |
| 94 i++; |
| 95 } |
| 96 // One after last-used |
| 97 i++; |
| 98 } |
| 99 |
| 100 // Loop through configurators until one succeeds to initialize |
| 101 while (i < CONFIGURATORS.size()) |
| 102 { |
| 103 try |
| 104 { |
| 105 final Class<?> clazz = CONFIGURATORS.get(i); |
| 106 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 107 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance(contex
t); |
| 108 if (pc.initialize()) |
| 109 { |
| 110 return pc; |
| 111 } |
| 112 } |
| 113 catch (final Exception e) |
| 114 { |
| 115 Log.d(TAG, "Configurator exception", e); |
| 116 // We don't need to handle exceptions here, only success matters |
| 117 } |
| 118 |
| 119 i++; |
| 120 } |
| 121 |
| 122 return null; |
| 123 } |
| 124 |
| 125 public static ProxyConfigurator registerProxy(final Context context, final Ine
tAddress address, final int port) |
| 126 { |
| 127 ProxyConfigurator pc = null; |
| 128 |
| 129 do |
| 130 { |
| 131 pc = initialize(context, pc); |
| 132 |
| 133 if (pc != null) |
| 134 { |
| 135 if (pc.registerProxy(address, port)) |
| 136 { |
| 137 Log.d(TAG, "Using '" + pc.toString() + "'"); |
| 138 return pc; |
| 139 } |
| 140 |
| 141 pc.shutdown(); |
| 142 } |
| 143 } |
| 144 while (pc != null); |
| 145 |
| 146 return null; |
| 147 } |
| 148 } |
OLD | NEW |