Skip to content

Commit

Permalink
Fix and rework Android error notifications (KhronosGroup#1051)
Browse files Browse the repository at this point in the history
* Clean up api sample base class interfaces

* Revert "Clean up api sample base class interfaces"

This reverts commit 7504395.

* Rework error handling and display on Android

Use a proper alert dialog displaying the last error message instead of a toast that just links to the log file
This method also does not require on any permissions (unlike toasts)
Fixes KhronosGroup#1050
  • Loading branch information
SaschaWillems authored Jun 5, 2024
1 parent ada3895 commit 23d68d6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2023, Arm Limited and Contributors
/* Copyright (c) 2019-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -17,11 +17,17 @@

package com.khronos.vulkan_samples;

import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.FileProvider;
Expand All @@ -32,11 +38,14 @@
import com.khronos.vulkan_samples.common.Notifications;

import java.io.File;
import java.util.concurrent.Semaphore;

public class NativeSampleActivity extends GameActivity {

private Context context;

private final Semaphore semaphore = new Semaphore(0, true);

@Override
protected void onCreate(Bundle instance) {
super.onCreate(instance);
Expand All @@ -62,34 +71,32 @@ public void onWindowFocusChanged(boolean hasFocus) {
}

/***
* Handle the application when an error is thrown
*
* @param log_file The location of the log output
* Handle the application when an error is thrown and display a message
*/
public void fatalError(final String log_file) {
File file = new File(log_file);

Uri path = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent.createChooser(intent, context.getResources().getString(R.string.open_file_with));
intent.setDataAndType(path, context.getContentResolver().getType(path));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Notifications.CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Vulkan Samples Error")
.setContentText("Fatal Error: click to view")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Log: " + log_file))
.setAutoCancel(true)
.setContentIntent(pi)
.setPriority(NotificationCompat.PRIORITY_HIGH);

NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.notify(Notifications.getNotificationId(this), builder.build());
public void fatalError(String message) {
final NativeSampleActivity activity = this;

ApplicationInfo applicationInfo = activity.getApplicationInfo();

this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Vulkan Samples");
builder.setMessage(message);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
semaphore.release();
}
});
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
});
try {
semaphore.acquire();
}
catch (InterruptedException e) { }
activity.finish();
}
}
5 changes: 4 additions & 1 deletion framework/platform/android/android_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,11 @@ void AndroidPlatform::terminate(ExitCode code)
log_output.clear();
break;
case ExitCode::FatalError:
send_notification(log_output);
{
const std::string error_message = "Error! Could not launch selected sample:" + get_last_error();
send_notification(error_message);
break;
}
default:
break;
}
Expand Down
32 changes: 22 additions & 10 deletions framework/platform/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ ExitCode Platform::main_loop_frame()

on_app_error(active_app->get_name());

if (app_requested())
{
LOGI("Attempting to load next application");
}
else
{
return ExitCode::FatalError;
}
}
}
if (app_requested())
{
LOGI("Attempting to load next application");
}
else
{
set_last_error(e.what());
return ExitCode::FatalError;
}
}
}

return ExitCode::Success;
}
Expand Down Expand Up @@ -232,6 +233,7 @@ ExitCode Platform::main_loop()
}
else
{
set_last_error(e.what());
return ExitCode::FatalError;
}
}
Expand Down Expand Up @@ -376,6 +378,11 @@ const std::string &Platform::get_temp_directory()
return temp_directory;
}

std::string &Platform::get_last_error()
{
return last_error;
}

Application &Platform::get_app()
{
assert(active_app && "Application is not valid");
Expand All @@ -398,6 +405,11 @@ void Platform::set_external_storage_directory(const std::string &dir)
external_storage_directory = dir;
}

void Platform::set_last_error(const std::string &error)
{
last_error = error;
}

std::vector<spdlog::sink_ptr> Platform::get_platform_sinks()
{
std::vector<spdlog::sink_ptr> sinks;
Expand Down
6 changes: 6 additions & 0 deletions framework/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Platform
*/
static const std::string &get_temp_directory();

std::string &get_last_error();

virtual void resize(uint32_t width, uint32_t height);

virtual void input_event(const InputEvent &input_event);
Expand All @@ -112,6 +114,8 @@ class Platform

static void set_external_storage_directory(const std::string &dir);

void set_last_error(const std::string &error);

template <class T>
T *get_plugin() const;

Expand Down Expand Up @@ -184,6 +188,8 @@ class Platform

std::vector<std::string> arguments;

std::string last_error;

// static so can be references from vkb::fs
static std::string external_storage_directory;

Expand Down

0 comments on commit 23d68d6

Please sign in to comment.