|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.matter.casting; |
| 18 | + |
| 19 | +import android.os.Bundle; |
| 20 | +import android.util.Log; |
| 21 | +import android.view.LayoutInflater; |
| 22 | +import android.view.View; |
| 23 | +import android.view.ViewGroup; |
| 24 | +import android.widget.Button; |
| 25 | +import android.widget.TextView; |
| 26 | +import androidx.annotation.Nullable; |
| 27 | +import androidx.fragment.app.Fragment; |
| 28 | +import com.R; |
| 29 | +import com.matter.casting.core.CastingPlayer; |
| 30 | +import com.matter.casting.support.DeviceTypeStruct; |
| 31 | +import com.matter.casting.support.EndpointFilter; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.Executors; |
| 35 | + |
| 36 | +/** A {@link Fragment} to Verify or establish a connection with a selected Casting Player. */ |
| 37 | +public class ConnectionExampleFragment extends Fragment { |
| 38 | + private static final String TAG = ConnectionExampleFragment.class.getSimpleName(); |
| 39 | + // Time (in sec) to keep the commissioning window open, if commissioning is required. |
| 40 | + // Must be >= 3 minutes. |
| 41 | + private static final long MIN_CONNECTION_TIMEOUT_SEC = 3 * 60; |
| 42 | + private final CastingPlayer targetCastingPlayer; |
| 43 | + private TextView connectionFragmentStatusTextView; |
| 44 | + private Button connectionFragmentNextButton; |
| 45 | + |
| 46 | + public ConnectionExampleFragment(CastingPlayer targetCastingPlayer) { |
| 47 | + Log.i(TAG, "ConnectionExampleFragment() called with target CastingPlayer"); |
| 48 | + this.targetCastingPlayer = targetCastingPlayer; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Use this factory method to create a new instance of this fragment using the provided |
| 53 | + * parameters. |
| 54 | + * |
| 55 | + * @return A new instance of fragment ConnectionExampleFragment. |
| 56 | + */ |
| 57 | + public static ConnectionExampleFragment newInstance(CastingPlayer castingPlayer) { |
| 58 | + Log.i(TAG, "newInstance() called"); |
| 59 | + return new ConnectionExampleFragment(castingPlayer); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onCreate(Bundle savedInstanceState) { |
| 64 | + super.onCreate(savedInstanceState); |
| 65 | + Log.i(TAG, "onCreate() called"); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public View onCreateView( |
| 70 | + LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 71 | + Log.i(TAG, "onCreateView() called"); |
| 72 | + // Inflate the layout for this fragment |
| 73 | + return inflater.inflate(R.layout.fragment_matter_connection_example, container, false); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 78 | + super.onViewCreated(view, savedInstanceState); |
| 79 | + Log.i(TAG, "onViewCreated() called"); |
| 80 | + |
| 81 | + connectionFragmentStatusTextView = getView().findViewById(R.id.connectionFragmentStatusText); |
| 82 | + connectionFragmentStatusTextView.setText( |
| 83 | + "Verifying or establishing connection with Casting Player with device name: " |
| 84 | + + targetCastingPlayer.getDeviceName()); |
| 85 | + |
| 86 | + connectionFragmentNextButton = getView().findViewById(R.id.connectionFragmentNextButton); |
| 87 | + Callback callback = (ConnectionExampleFragment.Callback) this.getActivity(); |
| 88 | + connectionFragmentNextButton.setOnClickListener( |
| 89 | + v -> { |
| 90 | + Log.i(TAG, "onViewCreated() NEXT clicked. Calling handleConnectionComplete()"); |
| 91 | + callback.handleConnectionComplete(targetCastingPlayer); |
| 92 | + }); |
| 93 | + |
| 94 | + Executors.newSingleThreadExecutor() |
| 95 | + .submit( |
| 96 | + () -> { |
| 97 | + Log.d(TAG, "onViewCreated() calling verifyOrEstablishConnection()"); |
| 98 | + |
| 99 | + EndpointFilter desiredEndpointFilter = |
| 100 | + new EndpointFilter(null, 65521, new ArrayList<DeviceTypeStruct>()); |
| 101 | + // The desired commissioning window timeout and EndpointFilter are optional. |
| 102 | + CompletableFuture<Void> completableFuture = |
| 103 | + targetCastingPlayer.VerifyOrEstablishConnection( |
| 104 | + MIN_CONNECTION_TIMEOUT_SEC, desiredEndpointFilter); |
| 105 | + |
| 106 | + Log.d(TAG, "onViewCreated() verifyOrEstablishConnection() called"); |
| 107 | + |
| 108 | + completableFuture |
| 109 | + .thenRun( |
| 110 | + () -> { |
| 111 | + Log.i( |
| 112 | + TAG, |
| 113 | + "CompletableFuture.thenRun(), connected to CastingPlayer with deviceId: " |
| 114 | + + targetCastingPlayer.getDeviceId()); |
| 115 | + getActivity() |
| 116 | + .runOnUiThread( |
| 117 | + () -> { |
| 118 | + connectionFragmentStatusTextView.setText( |
| 119 | + "Connected to Casting Player with device name: " |
| 120 | + + targetCastingPlayer.getDeviceName()); |
| 121 | + connectionFragmentNextButton.setEnabled(true); |
| 122 | + }); |
| 123 | + }) |
| 124 | + .exceptionally( |
| 125 | + exc -> { |
| 126 | + Log.e( |
| 127 | + TAG, |
| 128 | + "CompletableFuture.exceptionally(), CastingPLayer connection failed: " |
| 129 | + + exc.getMessage()); |
| 130 | + getActivity() |
| 131 | + .runOnUiThread( |
| 132 | + () -> { |
| 133 | + connectionFragmentStatusTextView.setText( |
| 134 | + "Casting Player connection failed due to: " |
| 135 | + + exc.getMessage()); |
| 136 | + }); |
| 137 | + return null; |
| 138 | + }); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + /** Interface for notifying the host. */ |
| 143 | + public interface Callback { |
| 144 | + /** Notifies listener to trigger transition on completion of connection */ |
| 145 | + void handleConnectionComplete(CastingPlayer castingPlayer); |
| 146 | + } |
| 147 | +} |
0 commit comments