Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tv-casting-app: Allow for a way for Matter TV casting cache to be purged #25665

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.DiscoveredNodeData;
Expand Down Expand Up @@ -77,6 +78,22 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
v -> callback.handleCommissioningButtonClicked(null);
manualCommissioningButton.setOnClickListener(manualCommissioningButtonOnClickListener);

Button purgeCacheButton = getView().findViewById(R.id.purgeCacheButton);
Context context = getContext().getApplicationContext();
View.OnClickListener purgeCacheOnClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean status = tvCastingApp.purgeCache();
Toast.makeText(
context,
"Cache purge " + (status ? "successful!" : "failed!"),
Toast.LENGTH_SHORT)
.show();
}
};
purgeCacheButton.setOnClickListener(purgeCacheOnClickListener);

ArrayAdapter<DiscoveredNodeData> arrayAdapter =
new VideoPlayerCommissionerAdapter(getActivity(), commissionerVideoPlayerList);
final ListView list = getActivity().findViewById(R.id.commissionerList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public native boolean verifyOrEstablishConnection(

public native List<VideoPlayer> getActiveTargetVideoPlayers();

public native boolean purgeCache();

/*
* CONTENT LAUNCHER CLUSTER
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ JNI_METHOD(jboolean, sendCommissioningRequest)(JNIEnv * env, jobject, jobject jD
return true;
}

JNI_METHOD(jboolean, purgeCache)(JNIEnv * env, jobject)
{
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "JNI_METHOD purgeCache called");

CHIP_ERROR err = CastingServer::GetInstance()->PurgeCache();
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "TVCastingApp-JNI::purgeCache failed: %" CHIP_ERROR_FORMAT, err.Format());
return false;
}
return true;
}

JNI_METHOD(jboolean, contentLauncherLaunchURL)
(JNIEnv * env, jobject, jobject contentApp, jstring contentUrl, jstring contentDisplayStr, jobject jResponseHandler)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
tools:layout_editor_absoluteY="1dp"
android:padding="10sp">

<Button
android:id="@+id/purgeCacheButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Purge Cache" />

<Button
android:id="@+id/manualCommissioningButton"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,16 @@
*/
- (void)disconnect:(dispatch_queue_t _Nonnull)clientQueue requestSentHandler:(nullable void (^)())requestSentHandler;

/**
/*!
@brief Purge data cached by the Matter casting library

@param clientQueue Queue to invoke callbacks on

@param responseHandler Called when purgeCache completes
*/
- (void)purgeCache:(dispatch_queue_t _Nonnull)clientQueue responseHandler:(void (^)(MatterError * _Nonnull))responseHandler;

/*!
@brief Start the Matter server and reconnect to a previously connected Video Player (if any). This API is async

@param clientQueue Queue to invoke callbacks on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,17 @@ - (void)disconnect:(dispatch_queue_t _Nonnull)clientQueue requestSentHandler:(nu
});
}

- (void)purgeCache:(dispatch_queue_t _Nonnull)clientQueue responseHandler:(void (^)(MatterError * _Nonnull))responseHandler
{
dispatch_sync(_chipWorkQueue, ^{
CHIP_ERROR err = CastingServer::GetInstance()->PurgeCache();
dispatch_async(clientQueue, ^{
responseHandler([[MatterError alloc] initWithCode:err.AsInteger()
message:[NSString stringWithUTF8String:err.AsString()]]);
});
});
}

- (void)contentLauncher_launchUrl:(ContentApp * _Nonnull)contentApp
contentUrl:(NSString * _Nonnull)contentUrl
contentDisplayStr:(NSString * _Nonnull)contentDisplayStr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ struct StartFromCacheView: View {

var body: some View {
VStack(alignment: .leading) {
Button("Purge cache", action: {
viewModel.purgeAndReReadCache()
})
.frame(width: 200, height: 30, alignment: .center)
.border(Color.black, width: 1)
.background(Color.blue)
.foregroundColor(Color.white)
.padding()

NavigationLink(
destination: CommissionerDiscoveryView(),
label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ class StartFromCacheViewModel: ObservableObject {
})
}
}

func purgeAndReReadCache() {
if let castingServerBridge = CastingServerBridge.getSharedInstance()
{
castingServerBridge.purgeCache(DispatchQueue.main, responseHandler: { (error: MatterError) -> () in
DispatchQueue.main.async {
self.Log.info("purgeCache returned \(error)")
self.readFromCache()
}
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CastingServer
std::function<void(TargetEndpointInfo *)> onNewOrUpdatedEndpoint);

void LogCachedVideoPlayers();
CHIP_ERROR PurgeVideoPlayerCache();
CHIP_ERROR PurgeCache();

/**
* Tears down all active subscriptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & ta
onConnectionFailure);
}

CHIP_ERROR CastingServer::PurgeVideoPlayerCache()
CHIP_ERROR CastingServer::PurgeCache()
{
return mPersistenceManager.PurgeVideoPlayerCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,11 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
CHIP_ERROR PersistenceManager::PurgeVideoPlayerCache()
{
ChipLogProgress(AppServer, "PersistenceManager::PurgeVideoPlayerCache called");
return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kCastingDataKey);
CHIP_ERROR err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(kCastingDataKey);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) // no error, if the key-value pair was not stored
{
ChipLogProgress(AppServer, "PersistenceManager::PurgeVideoPlayerCache ignoring error %" CHIP_ERROR_FORMAT, err.Format());
return CHIP_NO_ERROR;
}
return err;
}