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

Powered by Google App Engine
This is Rietveld