From eb689e8a481595283461bb8835ab50056c746aab Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Mon, 1 Mar 2021 19:04:42 +0800 Subject: [PATCH] feat:show information of script --- .../pub/doric/devkit/ui/DoricDevActivity.java | 36 +++++++++++++++++-- .../Devkit/Classes/DoricDevViewController.m | 22 ++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index 353bd196..c527ed5f 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -28,7 +28,11 @@ import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; import java.util.ArrayList; +import java.util.Locale; import pub.doric.Doric; import pub.doric.DoricContext; @@ -228,6 +232,27 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat } } + private static String toHex(byte[] bytes) { + char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + char[] resultCharArray = new char[bytes.length * 2]; + int index = 0; + for (byte b : bytes) { + resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; + resultCharArray[index++] = hexDigits[b & 0xf]; + } + return new String(resultCharArray); + } + + private static String md5(String s) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] bytes = md.digest(s.getBytes(Charset.forName("UTF-8"))); + return toHex(bytes); + } catch (Exception e) { + return ""; + } + } + private static class ContextCellAdapter extends RecyclerView.Adapter { private final ArrayList contexts = new ArrayList<>(); private Bitmap icon_off; @@ -285,9 +310,16 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat public void onClick(DialogInterface dialog, int which) { if (which == 0) { AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext(), R.style.Theme_Doric_Modal_Alert); - builder.setTitle("View source: " + context.getSource()); + builder.setTitle(String.format(Locale.getDefault(), + "View source: %s", + context.getSource())); String btnTitle = holder.itemView.getContext().getString(android.R.string.ok); - builder.setMessage(context.getScript()) + builder.setMessage( + String.format(Locale.getDefault(), + "Size:%d\nMD5:%s\nScript:\n%s", + context.getScript().length(), + md5(context.getScript()), + context.getScript())) .setPositiveButton(btnTitle, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 13af29c5..6b5a514b 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -21,6 +21,7 @@ // #import #import +#import #import "DoricDev.h" #import "DoricDevViewController.h" @@ -72,13 +73,30 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr return self; } +- (NSString *)md5:(NSString *)input { + const char *cStr = [input UTF8String]; + unsigned char digest[CC_MD5_DIGEST_LENGTH]; + CC_MD5(cStr, (CC_LONG) strlen(cStr), digest); + + NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; + for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { + [output appendFormat:@"%02x", digest[i]]; + } + return output; + +} + - (void)onClick { UIAlertController *alertController = [[UIAlertController alloc] init]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *viewSource = [UIAlertAction actionWithTitle:@"View source" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"View source: %@", self.doricContext.source] - message:self.doricContext.script - preferredStyle:UIAlertControllerStyleAlert]; + message:[NSString stringWithFormat:@"Size:%@\nMD5:%@\nScript:\n%@", + @(self.doricContext.script.length), + [self md5:self.doricContext.script], + self.doricContext.script] + preferredStyle: + UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:nil];