1 /* 2 * Copyright (C) 2023 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.settings.network.tether; 18 19 import android.app.Application; 20 import android.net.TetheringManager; 21 22 import androidx.annotation.NonNull; 23 import androidx.lifecycle.AndroidViewModel; 24 import androidx.lifecycle.LiveData; 25 import androidx.lifecycle.MutableLiveData; 26 import androidx.lifecycle.Transformations; 27 28 import java.util.List; 29 30 /** 31 * TetheringManager view model implementation 32 */ 33 public class TetheringManagerModel extends AndroidViewModel { 34 protected TetheringManager mTetheringManager; 35 protected EventCallback mEventCallback = new EventCallback(); 36 protected MutableLiveData<List<String>> mTetheredInterfaces = new MutableLiveData<>(); 37 protected StartTetheringCallback mStartTetheringCallback = new StartTetheringCallback(); 38 TetheringManagerModel(@onNull Application application)39 public TetheringManagerModel(@NonNull Application application) { 40 super(application); 41 mTetheringManager = application.getSystemService(TetheringManager.class); 42 mTetheringManager 43 .registerTetheringEventCallback(application.getMainExecutor(), mEventCallback); 44 } 45 46 @Override onCleared()47 protected void onCleared() { 48 mTetheringManager.unregisterTetheringEventCallback(mEventCallback); 49 } 50 51 /** 52 * Gets the TetheringManager If the system service is successfully obtained. 53 */ getTetheringManager()54 public TetheringManager getTetheringManager() { 55 return mTetheringManager; 56 } 57 58 /** 59 * Gets the TetheredInterfaces wrapped by LiveData. 60 */ 61 @NonNull getTetheredInterfaces()62 public LiveData<List<String>> getTetheredInterfaces() { 63 return Transformations.distinctUntilChanged(mTetheredInterfaces); 64 } 65 66 /** 67 * Starts tethering and runs tether provisioning for the given type if needed. If provisioning 68 * fails, stopTethering will be called automatically. 69 * 70 * @param type The tethering type, on of the {@code TetheringManager#TETHERING_*} constants. 71 */ startTethering(int type)72 public void startTethering(int type) { 73 mTetheringManager.startTethering(type, getApplication().getMainExecutor(), 74 mStartTetheringCallback); 75 } 76 77 /** 78 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if 79 * applicable. 80 * 81 * @param type The tethering type, on of the {@code TetheringManager#TETHERING_*} constants. 82 */ stopTethering(int type)83 public void stopTethering(int type) { 84 mTetheringManager.stopTethering(type); 85 } 86 87 /** 88 * Callback for use with {@link TetheringManager#registerTetheringEventCallback} to find out 89 * tethering upstream status. 90 */ 91 protected class EventCallback implements TetheringManager.TetheringEventCallback { 92 @Override onTetheredInterfacesChanged(List<String> interfaces)93 public void onTetheredInterfacesChanged(List<String> interfaces) { 94 mTetheredInterfaces.setValue(interfaces); 95 } 96 } 97 98 private class StartTetheringCallback implements TetheringManager.StartTetheringCallback { 99 @Override onTetheringStarted()100 public void onTetheringStarted() { 101 // Do nothing 102 } 103 104 @Override onTetheringFailed(int error)105 public void onTetheringFailed(int error) { 106 // Do nothing 107 } 108 } 109 } 110