diff --git a/exampleapp/src/main/java/com/org/matomo/demo/button/RText.java b/exampleapp/src/main/java/com/org/matomo/demo/button/RText.java index 76b917e56e64f8629cb2d2104afc7e82f83c7307..32b8fe52debc51a9d8a587ac7eb086982334e276 100644 --- a/exampleapp/src/main/java/com/org/matomo/demo/button/RText.java +++ b/exampleapp/src/main/java/com/org/matomo/demo/button/RText.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,6 +18,7 @@ package com.org.matomo.demo.button; import com.org.matomo.demo.chechbox.RHelper; import com.org.matomo.demo.chechbox.RTextHelper; + import ohos.agp.components.AttrSet; import ohos.agp.components.Text; import ohos.app.Context; @@ -30,41 +31,84 @@ import ohos.app.Context; public class RText extends Text implements RHelper { private RTextHelper mHelper; + /** + * RText + * + * @param context context + * @param attrSet attrSet + * @param styleName styleName + */ public RText(Context context, AttrSet attrSet, String styleName) { super(context, attrSet, styleName); } + /** + * RText + * + * @param context context + */ public RText(Context context) { this(context, null); } + /** + * RText + * + * @param context context + * @param attrs attrs + */ public RText(Context context, AttrSet attrs) { super(context, attrs); mHelper = new RTextHelper(context, this, attrs); } + /** + * RTextHelper + * + * @return RTextHelper + */ @Override public RTextHelper getHelper() { return mHelper; } + /** + * setEnabled + * + * @param isEnabled isEnabled + */ @Override public void setEnabled(boolean isEnabled) { super.setEnabled(isEnabled); if (mHelper != null) mHelper.setEnabled(isEnabled); } + /** + * setSelected + * + * @param isSelected isSelected + */ @Override public void setSelected(boolean isSelected) { if (mHelper != null) mHelper.setSelected(isSelected); super.setSelected(isSelected); } + /** + * isTextCursorVisible + * + * @return boolean + */ @Override public boolean isTextCursorVisible() { return super.isTextCursorVisible(); } + /** + * setVisibility + * + * @param visibility visibility + */ @Override public void setVisibility(int visibility) { if (mHelper != null) mHelper.onVisibilityChanged(null, visibility); diff --git a/exampleapp/src/main/java/com/org/matomo/demo/chechbox/AttrUtils.java b/exampleapp/src/main/java/com/org/matomo/demo/chechbox/AttrUtils.java index b9864bda75f4639823b0f18ce756a4b33aa25403..1c45ea28c30fb936a76a88f11b6154e212481f89 100644 --- a/exampleapp/src/main/java/com/org/matomo/demo/chechbox/AttrUtils.java +++ b/exampleapp/src/main/java/com/org/matomo/demo/chechbox/AttrUtils.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,9 +25,20 @@ import ohos.hiviewdfx.HiLogLabel; import java.util.NoSuchElementException; +/** + * AttrUtils + */ public class AttrUtils { static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x12345, "yuki"); + /** + * getBoolean + * + * @param attrs attrs + * @param attrName attrName + * @param isDefValue isDefValue + * @return boolean + */ public static boolean getBoolean(AttrSet attrs, String attrName, boolean isDefValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -37,6 +48,14 @@ public class AttrUtils { } } + /** + * getString + * + * @param attrs attrs + * @param attrName attrName + * @param defValue defValue + * @return String + */ public static String getString(AttrSet attrs, String attrName, String defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -46,6 +65,14 @@ public class AttrUtils { } } + /** + * getInt + * + * @param attrs attrs + * @param attrName attrName + * @param defValue defValue + * @return int + */ public static int getInt(AttrSet attrs, String attrName, int defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -55,6 +82,14 @@ public class AttrUtils { } } + /** + * getFloat + * + * @param attrs attrs + * @param attrName attrName + * @param defValue defValue + * @return float + */ public static float getFloat(AttrSet attrs, String attrName, float defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -64,6 +99,14 @@ public class AttrUtils { } } + /** + * getElement + * + * @param attrs attrs + * @param attrName attrName + * @param defValue defValue + * @return Element + */ public static Element getElement(AttrSet attrs, String attrName, Element defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -86,7 +129,7 @@ public class AttrUtils { /** * 获取getXmlManyValue * - * @param attrs attrs + * @param attrs attrs * @param attrName attrName * @param defValue defValue * @return 获取xml里面传递的多个值(也就是数组) @@ -100,6 +143,14 @@ public class AttrUtils { } } + /** + * getColor + * + * @param attrs attrs + * @param attrName attrName + * @param defValue defValue + * @return Color + */ public static Color getColor(AttrSet attrs, String attrName, Color defValue) { Attr attr = attrNoSuchElement(attrs, attrName); if (attr == null) { @@ -112,7 +163,7 @@ public class AttrUtils { /** * string 颜色值 转换 * - * @param attrs AttrSet + * @param attrs AttrSet * @param attrName 名称 * @param defValue 默认值 * @return 颜色 @@ -130,8 +181,8 @@ public class AttrUtils { /** * bool类型 * - * @param attrs AttrSet - * @param attrName 名称 + * @param attrs AttrSet + * @param attrName 名称 * @param isDefValue 默认值 * @return boolean */ diff --git a/exampleapp/src/main/java/com/org/matomo/demo/chechbox/ClipHelper.java b/exampleapp/src/main/java/com/org/matomo/demo/chechbox/ClipHelper.java index e54d6e0af792531bf2c58d3c638c6041bdda98f7..8bc8569151911a1c43d4a79261d2cb10311f4ac9 100644 --- a/exampleapp/src/main/java/com/org/matomo/demo/chechbox/ClipHelper.java +++ b/exampleapp/src/main/java/com/org/matomo/demo/chechbox/ClipHelper.java @@ -41,6 +41,9 @@ public class ClipHelper implements IClip { private Component mView; private boolean isClipLayout; + /** + * ClipHelper + */ public ClipHelper() { clipPaint.setAntiAlias(true); clipPaint.setColor(Color.BLUE); @@ -64,6 +67,11 @@ public class ClipHelper implements IClip { requestShapeUpdate(); } + /** + * dispatchDraw + * + * @param canvas canvas + */ @Override public void dispatchDraw(Canvas canvas) { if (!canClip()) return; @@ -74,6 +82,14 @@ public class ClipHelper implements IClip { canvas.drawPath(clipPath, clipPaint); } + /** + * onLayout + * + * @param left left + * @param top top + * @param right right + * @param bottom bottom + */ @Override public void onLayout(int left, int top, int right, int bottom) { if (!canClip()) return; @@ -99,6 +115,11 @@ public class ClipHelper implements IClip { getView().invalidate(); } + /** + * getView + * + * @return Component + */ public Component getView() { return mView; } diff --git a/tracker/src/main/java/org/matomo/sdk/Matomo.java b/tracker/src/main/java/org/matomo/sdk/Matomo.java index e33a19aa86df54ddd7ae5f5572c2a6ecfc444790..b071f501993627b2d098065aedfcb86a70ed8d7d 100644 --- a/tracker/src/main/java/org/matomo/sdk/Matomo.java +++ b/tracker/src/main/java/org/matomo/sdk/Matomo.java @@ -70,7 +70,7 @@ public class Matomo { if (newPrefs == null) { String prefName; try { - prefName = "org.matomo.sdk_" + Checksum.getMD5Checksum(tracker.getName()); + prefName = "org.matomo.sdk_" + Checksum.encode(tracker.getName()); } catch (Exception e) { prefName = "org.matomo.sdk_" + tracker.getName(); } diff --git a/tracker/src/main/java/org/matomo/sdk/dispatcher/EventDiskCache.java b/tracker/src/main/java/org/matomo/sdk/dispatcher/EventDiskCache.java index 00fe57ecdb8b486d6793912c3e5e6130f9a96c1a..d5ab9334ef0fa8496bfa68e779a9a035da443295 100644 --- a/tracker/src/main/java/org/matomo/sdk/dispatcher/EventDiskCache.java +++ b/tracker/src/main/java/org/matomo/sdk/dispatcher/EventDiskCache.java @@ -211,7 +211,8 @@ public class EventDiskCache { } } catch (IOException e) { Logger.error(e.getMessage()); - newFile.deleteOnExit(); + boolean delete = newFile.delete(); + Logger.error("Restored " +delete); return null; } finally { if (out != null) { @@ -228,7 +229,8 @@ public class EventDiskCache { if (isDataWritten) return newFile; else { //noinspection ResultOfMethodCallIgnored - newFile.delete(); + boolean delete = newFile.delete(); + Logger.error("Restored " +delete); return null; } } diff --git a/tracker/src/main/java/org/matomo/sdk/extra/DownloadTracker.java b/tracker/src/main/java/org/matomo/sdk/extra/DownloadTracker.java index ebfe2e9d0643f0a98057106586a8a1985e500910..0afd10220e04c60d0e58f7761c1a3ade4c64ebff 100644 --- a/tracker/src/main/java/org/matomo/sdk/extra/DownloadTracker.java +++ b/tracker/src/main/java/org/matomo/sdk/extra/DownloadTracker.java @@ -37,7 +37,7 @@ public class DownloadTracker {//todo 这个类先写死 * @return a string that will be used as extra identifier or null */ - String buildExtraIdentifier(); + File buildExtraIdentifier(); /** * The MD5 checksum of the apk file. @@ -63,10 +63,10 @@ public class DownloadTracker {//todo 这个类先写死 } @Override - public String buildExtraIdentifier() { + public File buildExtraIdentifier() { if (entryDir != null) { try { - return Checksum.getMD5Checksum(new File(entryDir)); + return Checksum.encode(new File(entryDir)); } catch (Exception e) { } } @@ -91,7 +91,7 @@ public class DownloadTracker {//todo 这个类先写死 } @Override - public String buildExtraIdentifier() { + public File buildExtraIdentifier() { return null; } } @@ -144,7 +144,7 @@ public class DownloadTracker {//todo 这个类先写死 StringBuilder installIdentifier = new StringBuilder(); installIdentifier.append("http://").append(mContext.getBundleName()).append(":").append(getVersion()); - String extraIdentifier = extra.buildExtraIdentifier(); + File extraIdentifier = extra.buildExtraIdentifier(); if (extraIdentifier != null) installIdentifier.append("/").append(extraIdentifier); //todo 先写成空 String referringApp = new PackageManagerImpl().getInstallerPackageName(PackageManagerImpl.TARGET_PACKAGE); diff --git a/tracker/src/main/java/org/matomo/sdk/tools/Checksum.java b/tracker/src/main/java/org/matomo/sdk/tools/Checksum.java index 6b9b3b42ee805774828d0412c6e8e900f6814c01..2bb628c2ba86a983ac5b92663eb390f2e59e6010 100644 --- a/tracker/src/main/java/org/matomo/sdk/tools/Checksum.java +++ b/tracker/src/main/java/org/matomo/sdk/tools/Checksum.java @@ -5,9 +5,7 @@ package org.matomo.sdk.tools; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; +import java.io.*; import java.security.MessageDigest; /** @@ -16,57 +14,306 @@ import java.security.MessageDigest; * @since 2021-03-25 */ public class Checksum { - private static final String HEXES = "0123456789ABCDEF"; + /** + * 功能:编码字符串 + * + * @param data 源字符串 + * @return String + */ + public static String encode(String data) { + return new String(encode(data.getBytes())); + } + + /** + * 功能:解码字符串 + * + * @param data 源字符串 + * @return String + */ + public static String decode(String data) { + return new String(decode(data.toCharArray())); + } /** - * Transforms byte into hex representation. + * 功能:编码byte[] * - * @param raw byte[] - * @return string + * @param data 源 + * @return char[] */ - public static String getHex(byte[] raw) { - if (raw == null) - return null; - final StringBuilder hex = new StringBuilder(2 * raw.length); - for (final byte b : raw) - hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))); - return hex.toString(); + public static char[] encode(byte[] data) { + char[] out = new char[((data.length + 2) / 3) * 4]; + for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { + boolean quad = false; + boolean trip = false; + + int val = (0xFF & (int) data[i]); + val <<= 8; + if ((i + 1) < data.length) { + val |= (0xFF & (int) data[i + 1]); + trip = true; + } + val <<= 8; + if ((i + 2) < data.length) { + val |= (0xFF & (int) data[i + 2]); + quad = true; + } + out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; + val >>= 6; + out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)]; + val >>= 6; + out[index + 1] = alphabet[val & 0x3F]; + val >>= 6; + out[index + 0] = alphabet[val & 0x3F]; + } + return out; + } + + /** + * 功能:解码 + * + * @param data 编码后的字符数组 + * @return byte[] + */ + public static byte[] decode(char[] data) { + + int tempLen = data.length; + for (int ix = 0; ix < data.length; ix++) { + if ((data[ix] > 255) || codes[data[ix]] < 0) { + --tempLen; // ignore non-valid chars and padding + } + } + // calculate required length: + // -- 3 bytes for every 4 valid base64 chars + // -- plus 2 bytes if there are 3 extra base64 chars, + // or plus 1 byte if there are 2 extra. + + int len = (tempLen / 4) * 3; + if ((tempLen % 4) == 3) { + len += 2; + } + if ((tempLen % 4) == 2) { + len += 1; + + } + byte[] out = new byte[len]; + + int shift = 0; // # of excess bits stored in accum + int accum = 0; // excess bits + int index = 0; + + // we now go through the entire array (NOT using the 'tempLen' value) + for (int ix = 0; ix < data.length; ix++) { + int value = (data[ix] > 255) ? -1 : codes[data[ix]]; + + if (value >= 0) { // skip over non-code + accum <<= 6; // bits shift up by 6 each time thru + shift += 6; // loop, with new bits being put in + accum |= value; // at the bottom. + if (shift >= 8) { // whenever there are 8 or more shifted in, + shift -= 8; // write them out (from the top, leaving any + out[index++] = // excess at the bottom for next iteration. + (byte) ((accum >> shift) & 0xff); + } + } + } + + // if there is STILL something wrong we just have to throw up now! + if (index != out.length) { + throw new Error("Miscalculated data length (wrote " + index + + " instead of " + out.length + ")"); + } + + return out; } /** - * MD5-Checksum for a string. + * 功能:编码文件 * - * @param string string - * @return string - * @throws Exception e + * @param file 源文件 */ - public static String getMD5Checksum(String string) throws Exception { - MessageDigest digest = MessageDigest.getInstance("MD5"); - digest.update(string.getBytes()); - byte[] messageDigest = digest.digest(); - return getHex(messageDigest); + public static File encode(File file) throws IOException { + if (!file.exists()) { + System.exit(0); + } + + else { + byte[] decoded = readBytes(file); + char[] encoded = encode(decoded); + writeChars(file, encoded); + } + file = null; + return file; + } + + /** + * 功能:解码文件。 + * + * @param file 源文件 + * @throws IOException IOException + */ + public static void decode(File file) throws IOException { + if (!file.exists()) { + System.exit(0); + } else { + char[] encoded = readChars(file); + byte[] decoded = decode(encoded); + writeBytes(file, decoded); + } + file = null; + } + + // + // code characters for values 0..63 + // + private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" + .toCharArray(); + + // + // lookup table for converting base64 characters to value in range 0..63 + // + private static byte[] codes = new byte[256]; + static { + for (int i = 0; i < 256; i++) { + codes[i] = -1; + // LoggerUtil.debug(i + "&" + codes[i] + " "); + } + for (int i = 'A'; i <= 'Z'; i++) { + codes[i] = (byte) (i - 'A'); + // LoggerUtil.debug(i + "&" + codes[i] + " "); + } + + for (int i = 'a'; i <= 'z'; i++) { + codes[i] = (byte) (26 + i - 'a'); + // LoggerUtil.debug(i + "&" + codes[i] + " "); + } + for (int i = '0'; i <= '9'; i++) { + codes[i] = (byte) (52 + i - '0'); + // LoggerUtil.debug(i + "&" + codes[i] + " "); + } + codes['+'] = 62; + codes['/'] = 63; + } + + private static byte[] readBytes(File file) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] b = null; + InputStream fis = null; + InputStream is = null; + try { + fis = new FileInputStream(file); + is = new BufferedInputStream(fis); + int count = 0; + byte[] buf = new byte[16384]; + while ((count = is.read(buf)) != -1) { + if (count > 0) { + baos.write(buf, 0, count); + } + } + b = baos.toByteArray(); + } finally { + try { + if (fis != null) + fis.close(); + if (is != null) + is.close(); + if (baos != null) + baos.close(); + } catch (Exception e) { + System.out.println(e); + } + } + return b; + } + + private static char[] readChars(File file) throws IOException { + CharArrayWriter caw = new CharArrayWriter(); + Reader fr = null; + Reader in = null; + try { + fr = new FileReader(file); + in = new BufferedReader(fr); + int count = 0; + char[] buf = new char[16384]; + while ((count = in.read(buf)) != -1) { + if (count > 0) { + caw.write(buf, 0, count); + } + } + + } finally { + try { + if (caw != null) + caw.close(); + if (in != null) + in.close(); + if (fr != null) + fr.close(); + } catch (Exception e) { + System.out.println(e); + } + } + + return caw.toCharArray(); + } + + private static void writeBytes(File file, byte[] data) throws IOException { + OutputStream fos = null; + OutputStream os = null; + try { + fos = new FileOutputStream(file); + os = new BufferedOutputStream(fos); + os.write(data); + + } finally { + try { + if (os != null) + os.close(); + if (fos != null) + fos.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + + private static void writeChars(File file, char[] data) throws IOException { + Writer fos = null; + Writer os = null; + try { + fos = new FileWriter(file); + os = new BufferedWriter(fos); + os.write(data); + + } finally { + try { + if (os != null) + os.close(); + if (fos != null) + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } } /** - * MD5-Checksum for a file. + * str * - * @param file file - * @return string - * @throws Exception e + * @param args str */ - public static String getMD5Checksum(File file) throws Exception { - if (!file.isFile()) - return null; - InputStream fis = new FileInputStream(file); - byte[] buffer = new byte[1024]; - MessageDigest complete = MessageDigest.getInstance("MD5"); - int numRead; - do { - numRead = fis.read(buffer); - if (numRead > 0) - complete.update(buffer, 0, numRead); - } while (numRead != -1); - fis.close(); - return getHex(complete.digest()); + public static void main(String[] args) { + String str = "5YWl5Y+j77ya6YeH6LSt566h55CGLT7kuJrliqHnrqHnkIYtPumHh+i0reiuouWNlQ0KPGltZyBz\n" + + "cmMgPSIke3BpY3R1cmVEb2NJcEFkcmVzc30vc2VydmljZS9DR0dMLVlXR0wtQ0dERC0yLTEucG5n\n" + + "Ij4NCuaTjeS9nOatpemqpO+8mg0KMeOAgQnlnKjjgJDph4fotK3orqLljZXjgJHpobXpnaItPuWP\n" + + "s+S4iuinkuOAkOaWsOWinuOAke+8mw0KPGltZyBzcmMgPSIke3BpY3R1cmVEb2NJcEFkcmVzc30v\n" + + "c2VydmljZS9DR0dMLVlXR0wtQ0dERC0yLTIucG5nIj4NCjLjgIEJ6YCJ5oup77mk5L6b5bqU5ZWG\n" + + "77ml77ybDQoz44CBCea3u+WKoO+5pOWVhuWTgeWQjeensO+5peOAgeiuoui0p++5pOaVsOmHj++5\n" + + "peOAge+5pOaIkOS6pOWNleS7t++5pe+8m+ezu+e7n+S8muiHquW4pu+5pOWNleaNrue8luWPt++5\n" + + "peOAge+5pOiuouWNleaXpeacn++5peOAge+5pOmHh+i0reS6uuWRmO+5peOAge+5pOmHh+i0remD\n" + + "qOmXqO+5pe+8jOS5n+S8muagueaNruiuoui0p+aVsOmHj+WSjOaIkOS6pOWNleS7t+iuoeeul+WH\n" + + "uu+5pOmHkemineWwj+iuoe+5peetieaVsOaNru+8mw0KNOOAgQnnvJbovpHlrozmiJDngrnlh7vj\n" + + "gJDkv53lrZjjgJHljbPlj6/jgIINCjxpbWcgc3JjID0iJHtwaWN0dXJlRG9jSXBBZHJlc3N9L3Nl\n" + + "cnZpY2UvQ0dHTC1ZV0dMLUNHREQtMi0zLnBuZyI+"; + System.out.println(decode(str)); } }