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

Commit 5c65738e authored by Peter Qiu's avatar Peter Qiu
Browse files

Osu2: setup ProvisionService

ProvisionService will be started when the OSU app is
started with "Provision" command.  This service will
run on a separate thread from the Main thread, to avoid
blocking on the Main thread.  This service doesn't
extend IntentService since it requires to perform
async operations.

Bug: 62388032
Test: am start -n com.android.osu/.MainActivity --es
      "com.android.osu.extra.COMMAND" "Provision"

Change-Id: I32c4eb53830a6b0404af085da98f9533346203eb
parent da277b23
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.osu;

public final class Constants {
    public static final String INTENT_EXTRA_COMMAND = "com.android.osu.extra.COMMAND";
    public static final String INTENT_EXTRA_OSU_PROVIDER = "com.android.osu.extra.OSU_PROVIDER";

    public static final String COMMAND_PROVISION = "Provision";
}
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
@@ -17,14 +17,60 @@
package com.android.osu;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.hotspot2.OsuProvider;
import android.os.Bundle;
import android.util.Log;

/**
 * Main entry point for the OSU (Online Sign-Up) app.
 */
public class MainActivity extends Activity {
    private static final String TAG = "OSU_MainActivity";
    private OsuService mService;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        Intent intent = getIntent();
        if (intent == null) {
            Log.e(TAG, "Intent not provided");
            finish();
        }

        if (!intent.hasExtra(Constants.INTENT_EXTRA_COMMAND)) {
            Log.e(TAG, "Command not provided");
            finish();
        }

        String command = intent.getStringExtra(Constants.INTENT_EXTRA_COMMAND);
        switch (command) {
            case Constants.COMMAND_PROVISION:
                if (!startProvisionService(intent.getParcelableExtra(
                        Constants.INTENT_EXTRA_OSU_PROVIDER))) {
                    finish();
                }
                break;
            default:
                Log.e(TAG, "Unknown command: '" + command + "'");
                finish();
                break;
        }
    }

    /**
     * Start the {@link ProvisionService} to perform provisioning tasks.
     *
     * @return true if service is started
     */
    private boolean startProvisionService(OsuProvider provider) {
        if (provider == null) {
            Log.e(TAG, "OSU Provider not provided");
            return false;
        }
        mService = new ProvisionService(this, provider);
        mService.start();
        return true;
    }
}
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.osu;

/**
 * Abstraction for services that can be performed by the OSU app, such as provisioning,
 * subscription remediation, and etc.
 */
public interface OsuService {
    /**
     * Start the service.
     */
    public void start();

    /**
     * Stop the service.
     */
    public void stop();
}
+82 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.osu;

import android.content.Context;
import android.net.wifi.hotspot2.OsuProvider;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 * Service responsible for performing Passpoint subscription provisioning tasks.
 * This service will run on a separate thread to avoid blocking on the Main thread.
 */
public class ProvisionService implements OsuService {
    private static final String TAG = "OSU_ProvisionService";
    private static final int COMMAND_START = 1;
    private static final int COMMAND_STOP = 2;

    private final Context mContext;
    private final HandlerThread mHandlerThread;
    private final ServiceHandler mServiceHandler;
    private final OsuProvider mProvider;

    public ProvisionService(Context context, OsuProvider provider) {
        mContext = context;
        mProvider = provider;
        mHandlerThread = new HandlerThread(TAG);
        mHandlerThread.start();
        mServiceHandler = new ServiceHandler(mHandlerThread.getLooper());
    }

    @Override
    public void start() {
        mServiceHandler.sendMessage(mServiceHandler.obtainMessage(COMMAND_START));
    }

    @Override
    public void stop() {
        mServiceHandler.sendMessage(mServiceHandler.obtainMessage(COMMAND_STOP));
    }

    /**
     * Handler class for handling commands to the ProvisionService.
     */
    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case COMMAND_START:
                    Log.e(TAG, "Start provision service");
                    break;
                case COMMAND_STOP:
                    Log.e(TAG, "Stop provision service");
                    break;
                default:
                    Log.e(TAG, "Unknown command: " + msg.what);
                    break;
            }
        }
    }
}