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

Side by Side Diff: src/org/adblockplus/android/compat/ProxyProperties.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.compat;
19
20 import android.content.Context;
21 import android.net.ConnectivityManager;
22
23 /**
24 * Compatibility wrapper for various {@code ProxyProperties}.
25 *
26 * @author rjeschke
27 */
28 public class ProxyProperties
29 {
30 private final static String DEFAULT_EXCL_LIST = null;
31
32 private final String host;
33 private final int port;
34 private final String exclusionList;
35
36 public ProxyProperties(final String host, final int port, final String excl)
37 {
38 this.host = host;
39 this.port = port;
40 this.exclusionList = excl;
41 }
42
43 public ProxyProperties(final String host, final int port)
44 {
45 this(host, port, DEFAULT_EXCL_LIST);
46 }
47
48 public String getHost()
49 {
50 return this.host;
51 }
52
53 public int getPort()
54 {
55 return this.port;
56 }
57
58 public String getExclusionList()
59 {
60 return this.exclusionList;
61 }
62
63 public Object toAndroidNetProxyProperties() throws CompatibilityException
64 {
65 try
66 {
67 return Class.forName("android.net.ProxyProperties")
68 .getConstructor(String.class, int.class, String.class)
69 .newInstance(this.host, Integer.valueOf(this.port), this.exclusionList );
70 }
71 catch (final Throwable t)
72 {
73 throw new CompatibilityException(t);
74 }
75 }
76
77 public Object toCmProxyProperties() throws CompatibilityException
78 {
79 try
80 {
81 return Class.forName("org.cyanogenmod.support.proxy.CmProxyProperties")
82 .getConstructor(String.class, int.class, String.class)
83 .newInstance(this.host, Integer.valueOf(this.port), this.exclusionList );
84 }
85 catch (final Throwable t)
86 {
87 throw new CompatibilityException(t);
88 }
89 }
90
91 public static ProxyProperties fromObject(final Object proxyProperties) throws CompatibilityException
92 {
93 if (proxyProperties == null)
94 {
95 return null;
96 }
97
98 try
99 {
100 final Class<?> clazz = proxyProperties.getClass();
101
102 final String host = (String) clazz.getMethod("getHost").invoke(proxyProper ties);
103 final int port = ((Number) clazz.getMethod("getPort").invoke(proxyProperti es)).intValue();
104 final String exlc = (String) clazz.getMethod("getExclusionList").invoke(pr oxyProperties);
105
106 return new ProxyProperties(host, port, exlc);
107 }
108 catch (final Throwable t)
109 {
110 throw new CompatibilityException(t);
111 }
112 }
113
114 public static ProxyProperties fromConnectivityManager(final ConnectivityManage r manager) throws CompatibilityException
115 {
116 try
117 {
118 return fromObject(manager.getClass()
119 .getMethod("getProxy")
120 .invoke(manager));
121 }
122 catch (final Throwable t)
123 {
124 throw new CompatibilityException(t);
125 }
126 }
127
128 public static ProxyProperties fromContext(final Context context)
129 {
130 try
131 {
132 return fromConnectivityManager((ConnectivityManager) context.getSystemServ ice(Context.CONNECTIVITY_SERVICE));
133 }
134 catch (final CompatibilityException e)
135 {
136 return null;
137 }
138 }
139
140 @Override
141 public String toString()
142 {
143 // Copied from android.net.ProxyProperties
144 final StringBuilder sb = new StringBuilder();
145
146 if (this.host != null)
147 {
148 sb.append("[");
149 sb.append(this.host);
150 sb.append("] ");
151 sb.append(Integer.toString(this.port));
152 if (this.exclusionList != null)
153 {
154 sb.append(" xl=").append(this.exclusionList);
155 }
156 }
157 else
158 {
159 sb.append("[ProxyProperties.mHost == null]");
160 }
161
162 return sb.toString();
163 }
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld