Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4359d564 authored by Jason Monk's avatar Jason Monk Committed by Android (Google) Code Review
Browse files

Merge "System binds PAC Local Proxy instead of self start" into klp-dev

parents b8d3a3de da205a74
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -13,12 +13,5 @@
            android:exported="true">
        </service>

        <receiver android:name=".ProxyServiceReceiver">

            <intent-filter>
                <action android:name="android.intent.action.PROXY_CHANGE" />
            </intent-filter>
        </receiver>

    </application>
</manifest>
+15 −9
Original line number Diff line number Diff line

/**
 * Copyright (c) 2013, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.proxyhandler;

import android.net.ProxyProperties;
import android.util.Log;

import com.google.android.collect.Lists;
@@ -36,7 +49,6 @@ public class ProxyServer extends Thread {
    public boolean mIsRunning = false;

    private ServerSocket serverSocket;
    private ProxyProperties mProxy;

    private class ProxyConnection implements Runnable {
        private Socket connection;
@@ -48,8 +60,6 @@ public class ProxyServer extends Thread {
        @Override
        public void run() {
            try {
                android.net.Proxy.setHttpProxySystemProperty(mProxy);

                String requestLine = getLine(connection.getInputStream());
                if (requestLine == null) {
                    connection.close();
@@ -212,8 +222,4 @@ public class ProxyServer extends Thread {
            }
        }
    }

    public void setProxy(ProxyProperties proxy) {
        mProxy = proxy;
    }
}
+18 −29
Original line number Diff line number Diff line
/**
 * Copyright (c) 2013, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.proxyhandler;

import android.app.Service;
@@ -18,43 +33,17 @@ public class ProxyService extends Service {
    /** Keep these values up-to-date with PacManager.java */
    public static final String KEY_PROXY = "keyProxy";
    public static final String HOST = "localhost";
    // STOPSHIP This being a static port means it can be hijacked by other apps.
    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) {
    public void onCreate() {
        super.onCreate();
        if (server == null) {
            server = new ProxyServer();
            server.startServer();
        }
        server.setProxy(proxy);
    }

    @Override
+0 −29
Original line number Diff line number Diff line
package com.android.proxyhandler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;
import android.net.ProxyProperties;
import android.os.Bundle;
import android.text.TextUtils;

public class ProxyServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, ProxyService.class);
        Bundle bundle = intent.getExtras();
        ProxyProperties proxy = null;
        if (bundle != null) {
            proxy = bundle.getParcelable(Proxy.EXTRA_PROXY_INFO);
            service.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
        }
        if ((proxy != null) && (!TextUtils.isEmpty(proxy.getPacFileUrl()))) {
            context.startService(service);
        } else {
            context.stopService(service);
        }
    }

}
+24 −7
Original line number Diff line number Diff line
@@ -48,10 +48,12 @@ import java.net.URLConnection;
 * @hide
 */
public class PacManager {
    public static final String PROXY_PACKAGE = "com.android.pacprocessor";
    public static final String PROXY_SERVICE = "com.android.pacprocessor.PacService";
    public static final String PROXY_SERVICE_NAME = "com.android.net.IProxyService";
    public static final String PAC_PACKAGE = "com.android.pacprocessor";
    public static final String PAC_SERVICE = "com.android.pacprocessor.PacService";
    public static final String PAC_SERVICE_NAME = "com.android.net.IProxyService";

    public static final String PROXY_PACKAGE = "com.android.proxyhandler";
    public static final String PROXY_SERVICE = "com.android.proxyhandler.ProxyService";

    private static final String TAG = "PacManager";

@@ -73,6 +75,7 @@ public class PacManager {
    private IProxyService mProxyService;
    private PendingIntent mPacRefreshIntent;
    private ServiceConnection mConnection;
    private ServiceConnection mProxyConnection;
    private Context mContext;

    private int mCurrentDelay;
@@ -229,7 +232,7 @@ public class PacManager {
            return;
        }
        Intent intent = new Intent();
        intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE);
        intent.setClassName(PAC_PACKAGE, PAC_SERVICE);
        mConnection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName component) {
@@ -242,12 +245,12 @@ public class PacManager {
            public void onServiceConnected(ComponentName component, IBinder binder) {
                synchronized (mProxyLock) {
                    try {
                        Log.d(TAG, "Adding service " + PROXY_SERVICE_NAME + " "
                        Log.d(TAG, "Adding service " + PAC_SERVICE_NAME + " "
                                + binder.getInterfaceDescriptor());
                    } catch (RemoteException e1) {
                        Log.e(TAG, "Remote Exception", e1);
                    }
                    ServiceManager.addService(PROXY_SERVICE_NAME, binder);
                    ServiceManager.addService(PAC_SERVICE_NAME, binder);
                    mProxyService = IProxyService.Stub.asInterface(binder);
                    if (mProxyService == null) {
                        Log.e(TAG, "No proxy service");
@@ -262,13 +265,27 @@ public class PacManager {
                }
            }
        };
        Log.e(TAG, "Attempting to bind");
        mContext.bindService(intent, mConnection,
                Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE);

        intent = new Intent();
        intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE);
        mProxyConnection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName component) {
            }

            @Override
            public void onServiceConnected(ComponentName component, IBinder binder) {
            }
        };
        mContext.bindService(intent, mProxyConnection,
                Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE);
    }

    private void unbind() {
        mContext.unbindService(mConnection);
        mContext.unbindService(mProxyConnection);
        mConnection = null;
    }
}