Skip to content

Commit

Permalink
bump to1.8.5, upgrade ncnn 20230816, add notification
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Aug 26, 2023
1 parent a21c4ec commit c7786e4
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/3rdparty/tests/*
/.idea/
*.apk
*.zip
4 changes: 2 additions & 2 deletions RealSR-NCNN-Android-GUI/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.tumuyan.ncnn.realsr"
minSdk 24
targetSdk 28
versionCode 32
versionName "1.8.4"
versionCode 33
versionName '1.8.5'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.icu.text.SimpleDateFormat;
Expand Down Expand Up @@ -43,14 +47,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;


public class MainActivity extends AppCompatActivity {
private static final int SELECT_IMAGE = 1;
Expand Down Expand Up @@ -114,12 +117,15 @@ public class MainActivity extends AppCompatActivity {
};
private int tileSize;
private boolean useCPU;
private boolean keepScreen;
private boolean keepScreen, useNotification;
private boolean prePng;
private boolean autoSave;
private boolean showSearchView;
private String savePath = galleryPath;

private NotificationManager mNManager;
private static final int NOTIFY_ID = 1;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
Expand Down Expand Up @@ -169,24 +175,18 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (!run_fake_command(q)) {
stopCommand();
String finalImageName = imageName;
new Thread(
() -> {
run20(q);
final File finalfile = new File(dir + finalImageName);
if (finalfile.exists() && (!finalfile.isDirectory())) {
runOnUiThread(
() -> {
imageView.setVisibility(View.VISIBLE);
imageView.setImage(ImageSource.uri(finalfile.getAbsolutePath()));
}
);
} else {
runOnUiThread(
() -> imageView.setVisibility(View.GONE)
);
}
}
).start();
new Thread(() -> {
run20(q);
final File finalfile = new File(dir + finalImageName);
if (finalfile.exists() && (!finalfile.isDirectory())) {
runOnUiThread(() -> {
imageView.setVisibility(View.VISIBLE);
imageView.setImage(ImageSource.uri(finalfile.getAbsolutePath()));
});
} else {
runOnUiThread(() -> imageView.setVisibility(View.GONE));
}
}).start();
}

return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -283,6 +283,7 @@ public void onResume() {
tileSize = mySharePerferences.getInt("tileSize", 0);
threadCount = mySharePerferences.getString("threadCount", "");
keepScreen = mySharePerferences.getBoolean("keepScreen", false);
useNotification = mySharePerferences.getBoolean("useNotification", false);
prePng = mySharePerferences.getBoolean("PrePng", true);
useCPU = mySharePerferences.getBoolean("useCPU", false);
autoSave = mySharePerferences.getBoolean("autoSave", false);
Expand Down Expand Up @@ -409,15 +410,9 @@ private List<String> getExtraCommands(String extraPath, String extraCommand) {
List<String> cmdLabel = new ArrayList<>();

// 增加resize-ncnn经典插值放大的命令
String[] classicalFilters = {
"bicubic",
"avir",
"avir-lancir",
};
String[] classicalFilters = {"bicubic", "avir", "avir-lancir",};

String[] classicalResize = {
"2", "4"
};
String[] classicalResize = {"2", "4"};

for (String f : classicalFilters) {
for (String s : classicalResize) {
Expand All @@ -441,9 +436,7 @@ private List<String> getExtraCommands(String extraPath, String extraCommand) {
"Blackman",
};

String[] magickResize = {
"200%", "400%", "1000%"
};
String[] magickResize = {"200%", "400%", "1000%"};

for (String f : magickFilters) {
for (String s : magickResize) {
Expand Down Expand Up @@ -585,7 +578,7 @@ protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
} else if (orientation == 2)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else if(orientation==3) {
else if (orientation == 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Expand Down Expand Up @@ -723,12 +716,18 @@ public boolean onQueryTextChange(String newText) {
overridePendingTransition(0, android.R.anim.slide_out_right);
});

mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// 创建渠道
NotificationChannel channel = new NotificationChannel("message", "message", NotificationManager.IMPORTANCE_HIGH);
mNManager.createNotificationChannel(channel);
}


requirePremision();

if (menuProgress != null)
menuProgress.setTitle("");
else
initProcess = true;
if (menuProgress != null) menuProgress.setTitle("");
else initProcess = true;

readFileFromShare();
}
Expand All @@ -752,6 +751,29 @@ private void requirePremision() {
}


private void notify(Context mContext, String text) {
if (!useNotification)
return;

if (text == null) {
mNManager.cancel(NOTIFY_ID); //取消Notification
return;
}

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, "message");
mBuilder.setContentTitle(getString(R.string.app_name)) //标题
.setContentText(text)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.FLAG_ONGOING_EVENT)
// .setAutoCancel(true) //设置点击后取消Notification
;
Notification notify = mBuilder.build();
mNManager.notify(NOTIFY_ID, notify);

}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST) {
Expand Down Expand Up @@ -848,7 +870,11 @@ public synchronized boolean run20(@NonNull String cmd) {
cmd = cmd.replace(" output.png ", " '" + savePath + "' ");
}
}
runOnUiThread(() -> menuProgress.setTitle(BUSY));

runOnUiThread(() -> {
menuProgress.setTitle(BUSY);
notify(this, BUSY);
});
modelName = "Real-ESRGAN-anime";
if (cmd.matches(".+\\s-m(\\s+)[^\\s]*models-.+")) {
modelName = cmd.replaceFirst(".+\\s-m(\\s+)[^\\s]*models-([^\\s]+).*", "$2");
Expand Down Expand Up @@ -933,10 +959,13 @@ public synchronized boolean run20(@NonNull String cmd) {
boolean p = run_ncnn && line.matches("\\s*\\d([0-9.]*)%(\\s.+)?");
progressText = line.trim().split("\\s")[0];

String finalLine = line;
runOnUiThread(() -> {
logTextView.setText(result);
if (p)
if (p) {
menuProgress.setTitle(progressText);
notify(this, finalLine);
}
});

Log.d("run20 errorResult", line);
Expand All @@ -963,10 +992,13 @@ public synchronized boolean run20(@NonNull String cmd) {
boolean p = run_ncnn && line.matches("\\d([0-9.]*)%");
progressText = line;

String finalLine = line;
runOnUiThread(() -> {
logTextView.setText(result);
if (p)
if (p) {
menuProgress.setTitle(progressText);
notify(this, finalLine);
}
});

Log.d("run20 successResult", line);
Expand All @@ -984,14 +1016,14 @@ public synchronized boolean run20(@NonNull String cmd) {

} catch (Exception e) {
e.printStackTrace();
notify(this, getString(R.string.error));
return false;
}
Log.d("run_20", "finish, process " + (process != null));

try {
Log.d("run_20", "finish, exitValue " + process.exitValue());
if (process.exitValue() != 0)
process.destroy();
if (process.exitValue() != 0) process.destroy();
} catch (Exception e) {
// e.printStackTrace();
}
Expand All @@ -1011,14 +1043,14 @@ public synchronized boolean run20(@NonNull String cmd) {
if (run_ncnn) {
result.append(", ").append(modelName + "\n");
Log.i("run20 finish'", "run_ncnn=" + run_ncnn + ", modelName=" + modelName + ", ..." + result.substring(Math.max(result.length() - 100, 0)));
} else
Log.i("run20 finish", "run_ncnn=false");
} else Log.i("run20 finish", "run_ncnn=false");

boolean finalOutput_savePath = output_savePath;
runOnUiThread(() -> {

logTextView.setText(result);
menuProgress.setTitle(getResources().getString(R.string.done));
notify(this, getString(R.string.done));

if (save) {
if (!outputFile.exists()) {
Expand All @@ -1039,17 +1071,15 @@ public synchronized boolean run20(@NonNull String cmd) {
private void stopCommand() {
if (process != null) {
process.destroy();
if (menuProgress != null)
menuProgress.setTitle("");
if (menuProgress != null) menuProgress.setTitle("");
}
newTask = true;
}

private boolean saveInputImage(@NonNull InputStream in, String path) {

Log.i("saveInputImage", "start ");
if (path.isEmpty())
path = dir + "/input.png";
if (path.isEmpty()) path = dir + "/input.png";
File file = new File(path);

if (file.exists()) {
Expand Down Expand Up @@ -1097,8 +1127,7 @@ private boolean saveInputImage(@NonNull InputStream in, String path) {
} catch (IOException e) {
e.printStackTrace();
}
} else
run20("./magick tmp '" + path + "'");
} else run20("./magick tmp '" + path + "'");
}

} catch (IOException e) {
Expand All @@ -1117,28 +1146,23 @@ private boolean saveInputImage(@NonNull InputStream in, String path) {
private void updateImage(final String path, String text, boolean keepScreen) {
Log.i("saveInputImage", "runOnUiThread");
File file = new File(path);
runOnUiThread(
() -> {
if (file.exists() && (!file.isDirectory())) {
imageView.setVisibility(View.VISIBLE);
imageView.setImage(ImageSource.uri(path));
logTextView.setText(text);
Log.i("saveInputImage", "finish");
} else
Log.i("saveInputImage", "skip");
if (keepScreen) {
logTextView.setKeepScreenOn(false);
}
runOnUiThread(() -> {
if (file.exists() && (!file.isDirectory())) {
imageView.setVisibility(View.VISIBLE);
imageView.setImage(ImageSource.uri(path));
logTextView.setText(text);
Log.i("saveInputImage", "finish");
} else Log.i("saveInputImage", "skip");
if (keepScreen) {
logTextView.setKeepScreenOn(false);
}

}
);
});
}

private boolean run_fake_command(String q) {
if (q == null)
return true;
if (q.isEmpty())
return true;
if (q == null) return true;
if (q.isEmpty()) return true;
if (q.equals("help")) {
logTextView.setText(getString(R.string.default_log));
showImage(null, "");
Expand All @@ -1155,14 +1179,12 @@ private boolean run_fake_command(String q) {
}
showImage(file, getString(R.string.show) + path);

} else
return false;
} else return false;
return true;
}

private void showImage(File file, String info) {
if (file == null)
imageView.setVisibility(View.GONE);
if (file == null) imageView.setVisibility(View.GONE);
else if (file.exists() && (!file.isDirectory())) {
imageView.setVisibility(View.VISIBLE);
imageView.setImage(ImageSource.uri(file.getAbsolutePath()));
Expand Down Expand Up @@ -1238,8 +1260,7 @@ private String saveOutputCmd() {
String q = formats[format].replaceAll("[a-zA-Z%\\s]+", "");
if (q.length() > 0) {
cmd = ("./magick output.png -quality " + q);
} else
cmd = ("./magick output.png");
} else cmd = ("./magick output.png");
}
}

Expand Down
Loading

0 comments on commit c7786e4

Please sign in to comment.