summaryrefslogtreecommitdiffstats
path: root/packages/services/Proxy/src/com/android/proxyhandler/ProxyService.java
blob: 18ed645f222346023d6810cf2afc042da5bac503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.android.proxyhandler;

import android.app.Service;
import android.content.Intent;
import android.net.Proxy;
import android.net.ProxyProperties;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;

/**
 * @hide
 */
public class ProxyService extends Service {

    private static ProxyServer server = null;

    /** Keep these values up-to-date with PacManager.java */
    public static final String KEY_PROXY = "keyProxy";
    public static final String HOST = "localhost";
    public static final int PORT = 8182;
    public static final String EXCL_LIST = "";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            if (handleCommand(intent)) {
                return START_REDELIVER_INTENT;
            }
        }
        return START_NOT_STICKY;
    }

    private boolean handleCommand(Intent intent) {
        Bundle bundle = intent.getExtras();
        ProxyProperties proxy = null;
        if ((bundle != null) && bundle.containsKey(Proxy.EXTRA_PROXY_INFO)) {
            proxy = bundle.getParcelable(Proxy.EXTRA_PROXY_INFO);
            if ((proxy != null) && !TextUtils.isEmpty(proxy.getPacFileUrl())) {
                startProxy(proxy);
                return true;
            } else {
                stopSelf();
            }
        } else {
            stopSelf();
        }
        return false;
    }


    private void startProxy(ProxyProperties proxy) {
        if (server == null) {
            server = new ProxyServer();
            server.startServer();
        }
        server.setProxy(proxy);
    }

    @Override
    public void onDestroy() {
        if (server != null) {
            server.stopServer();
            server = null;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}