Home | History | Annotate | Download | only in osu
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.osu;
     18 
     19 import android.content.Context;
     20 import android.net.Network;
     21 import android.net.wifi.hotspot2.OsuProvider;
     22 import android.os.Handler;
     23 import android.os.HandlerThread;
     24 import android.os.Looper;
     25 import android.os.Message;
     26 import android.util.Log;
     27 
     28 import java.io.IOException;
     29 
     30 /**
     31  * Service responsible for performing Passpoint subscription provisioning tasks.
     32  * This service will run on a separate thread to avoid blocking on the Main thread.
     33  */
     34 public class ProvisionService implements OsuService {
     35     private static final String TAG = "OSU_ProvisionService";
     36     private static final int COMMAND_START = 1;
     37     private static final int COMMAND_STOP = 2;
     38 
     39     private final Context mContext;
     40     private final HandlerThread mHandlerThread;
     41     private final ServiceHandler mServiceHandler;
     42     private final OsuProvider mProvider;
     43 
     44     private boolean mStarted = false;
     45     private NetworkConnection mNetworkConnection = null;
     46 
     47     public ProvisionService(Context context, OsuProvider provider) {
     48         mContext = context;
     49         mProvider = provider;
     50         mHandlerThread = new HandlerThread(TAG);
     51         mHandlerThread.start();
     52         mServiceHandler = new ServiceHandler(mHandlerThread.getLooper());
     53     }
     54 
     55     @Override
     56     public void start() {
     57         mServiceHandler.sendMessage(mServiceHandler.obtainMessage(COMMAND_START));
     58     }
     59 
     60     @Override
     61     public void stop() {
     62         mServiceHandler.sendMessage(mServiceHandler.obtainMessage(COMMAND_STOP));
     63     }
     64 
     65     /**
     66      * Handler class for handling commands to the ProvisionService.
     67      */
     68     private final class ServiceHandler extends Handler {
     69         public ServiceHandler(Looper looper) {
     70             super(looper);
     71         }
     72 
     73         @Override
     74         public void handleMessage(Message msg) {
     75             switch (msg.what) {
     76                 case COMMAND_START:
     77                     if (mStarted) {
     78                         Log.e(TAG, "Service already started");
     79                         return;
     80                     }
     81                     try {
     82                         // Initiate network connection to the OSU AP.
     83                         mNetworkConnection = new NetworkConnection(
     84                                 mContext, this, mProvider.getOsuSsid(),
     85                                 mProvider.getNetworkAccessIdentifier(), new NetworkCallbacks());
     86                         mStarted = true;
     87                     } catch (IOException e) {
     88                         // TODO(zqiu): broadcast failure event via LocalBroadcastManager.
     89                     }
     90                     break;
     91                 case COMMAND_STOP:
     92                     if (!mStarted) {
     93                         Log.e(TAG, "Service not started");
     94                         return;
     95                     }
     96                     Log.e(TAG, "Stop provision service");
     97                     break;
     98                 default:
     99                     Log.e(TAG, "Unknown command: " + msg.what);
    100                     break;
    101             }
    102         }
    103     }
    104 
    105     private final class NetworkCallbacks implements NetworkConnection.Callbacks {
    106         @Override
    107         public void onConnected(Network network) {
    108             Log.d(TAG, "Connected to OSU AP");
    109         }
    110 
    111         @Override
    112         public void onDisconnected() {
    113         }
    114 
    115         @Override
    116         public void onTimeout() {
    117         }
    118     }
    119 }
    120