开发者社区> 问答> 正文

引发异常Android Studio后处理应用程序崩溃

我遇到此代码的问题。女巫(开源)

public class Token {

public class ErrorHandler extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));

    }}


public static class TokenUriInvalidException extends Exception{

    private static final long serialVersionUID = -1108624734612362345L;




}


public static enum TokenType {
    HOTP, TOTP
}

private static char[] STEAMCHARS = new char[] {
        '2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C',
        'D', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q',
        'R', 'T', 'V', 'W', 'X', 'Y'};

private String issuerInt;
private String issuerExt;
private String issuerAlt;
private String label;
private String labelAlt;
private String image;
private String imageAlt;
private TokenType type;
private String algo;
private byte[] secret;
private int digits;
private long counter;
private int period;



private Token(Uri uri, boolean internal) throws TokenUriInvalidException {

    validateTokenURI(uri);

    String path = uri.getPath();
    // Strip the path of its leading '/'
    path = path.replaceFirst("/","");

    if (path.length() == 0)
    throw new TokenUriInvalidException();


    int i = path.indexOf(':');
    issuerExt = i < 0 ? "" : path.substring(0, i);
    issuerInt = uri.getQueryParameter("issuer");
    label = path.substring(i >= 0 ? i + 1 : 0);

    algo = uri.getQueryParameter("algorithm");
    if (algo == null)
        algo = "sha1";
    algo = algo.toUpperCase(Locale.US);
    try {
        Mac.getInstance("Hmac" + algo);
    } catch (NoSuchAlgorithmException e1) {
        throw new TokenUriInvalidException();
    }

    try {
        String d = uri.getQueryParameter("digits");
        if (d == null)
            d = "6";
        digits = Integer.parseInt(d);
        if (!issuerExt.equals("Steam") && digits != 6 && digits != 8)
            throw new TokenUriInvalidException();
    } catch (NumberFormatException e) {
        throw new TokenUriInvalidException();
    }

    try {
        String p = uri.getQueryParameter("period");
        if (p == null)
            p = "30";
        period = Integer.parseInt(p);
        period = (period > 0) ? period : 30; // Avoid divide-by-zero
    } catch (NumberFormatException e) {
        throw new TokenUriInvalidException();
    }

    if (type == TokenType.HOTP) {
        try {
            String c = uri.getQueryParameter("counter");
            if (c == null)
                c = "0";
            counter = Long.parseLong(c);
        } catch (NumberFormatException e) {
            throw new TokenUriInvalidException();
        }
    }

    try {
        String s = uri.getQueryParameter("secret");
        secret = Base32String.decode(s);
    } catch (DecodingException e) {
        throw new TokenUriInvalidException();
    } catch (NullPointerException e) {
        throw new TokenUriInvalidException();
    }

    image = uri.getQueryParameter("image");

    if (internal) {
        setIssuer(uri.getQueryParameter("issueralt"));
        setLabel(uri.getQueryParameter("labelalt"));
    }

    boolean isDateExpired;
    if(!label.equals("")) {
        isDateExpired=QRcodeExpired();
        if(!isDateExpired){
            throw new TokenUriInvalidException();}}
}

/ * public void showNotvalid(){

Intent i = new Intent(this, AlerntActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}*/

public boolean QRcodeExpired () {
  String ExpiredDate = label;
  String SystemDate;
  Calendar calendar = Calendar.getInstance();
  SystemDate = DateFormat.getDateInstance().format(calendar.getTime());
  return ExpiredDate.equals(SystemDate);
}

private void validateTokenURI(Uri uri) throws TokenUriInvalidException{


  if (uri == null) throw new TokenUriInvalidException();

  if (uri.getScheme() == null||!(uri.getScheme().equals("otpauth")||uri.getScheme().equals("taqnia"))){
      throw new TokenUriInvalidException();
     // new ErrorHandler();
  }




      if (uri.getAuthority() == null) throw new TokenUriInvalidException();

      if (uri.getAuthority().equals("totp")) {
          type = TokenType.TOTP;
      } else if (uri.getAuthority().equals("hotp"))
          type = TokenType.HOTP;
      else {
          throw new TokenUriInvalidException();
      }

      if (uri.getPath() == null)
          throw new TokenUriInvalidException();

}

private String getHOTP(long counter) {
  // Encode counter in network byte order
  ByteBuffer bb = ByteBuffer.allocate(8);
  bb.putLong(counter);

  // Create digits divisor
  int div = 1;
  for (int i = digits; i > 0; i--)
      div *= 10;

  // Create the HMAC
  try {
      Mac mac = Mac.getInstance("Hmac" + algo);
      mac.init(new SecretKeySpec(secret, "Hmac" + algo));

      // Do the hashing
      byte[] digest = mac.doFinal(bb.array());

      // Truncate
      int binary;
      int off = digest[digest.length - 1] & 0xf;
      binary = (digest[off] & 0x7f) << 0x18;
      binary |= (digest[off + 1] & 0xff) << 0x10;
      binary |= (digest[off + 2] & 0xff) << 0x08;
      binary |= (digest[off + 3] & 0xff);

      String hotp = "";
      if (issuerExt.equals("Steam")) {
          for (int i = 0; i < digits; i++) {
              hotp += STEAMCHARS[binary % STEAMCHARS.length];
              binary /= STEAMCHARS.length;
          }
      } else {
          binary = binary % div;

          // Zero pad
          hotp = Integer.toString(binary);
          while (hotp.length() != digits)
              hotp = "0" + hotp;
      }

      return hotp;
  } catch (InvalidKeyException e) {
      e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
  }

  return "";
}

public Token(String uri, boolean internal) throws TokenUriInvalidException {
  this(Uri.parse(uri), internal);
}

public Token(Uri uri) throws TokenUriInvalidException {
  this(uri, false);
}

public Token(String uri) throws TokenUriInvalidException {
  this(Uri.parse(uri));
}

public String getID() {
  String id;
  if (issuerInt != null && !issuerInt.equals(""))
      id = issuerInt + ":" + label;
  else if (issuerExt != null && !issuerExt.equals(""))
      id = issuerExt + ":" + label;
  else
      id = label;

  return id;
}

// NOTE: This changes internal data. You MUST save the token immediately.
public void setIssuer(String issuer) {
  issuerAlt = (issuer == null || issuer.equals(this.issuerExt)) ? null : issuer;
}

public String getIssuer() {
  if (issuerAlt != null)
      return issuerAlt;
  return issuerExt != null ? issuerExt : "";
}

// NOTE: This changes internal data. You MUST save the token immediately.
public void setLabel(String label) {
  labelAlt = (label == null || label.equals(this.label)) ? null : label;
}

public String getLabel() {
  if (labelAlt != null)
      return labelAlt;
  return label != null ? label : "";
}

public int getDigits() {
  return digits;
}

// NOTE: This may change internal data. You MUST save the token immediately.
public TokenCode generateCodes() {
  long cur = System.currentTimeMillis();

  switch (type) {
  case HOTP:
      return new TokenCode(getHOTP(counter++), cur, cur + (period * 1000));

  case TOTP:
      long counter = cur / 1000 / period;
      return new TokenCode(getHOTP(counter + 0),
                           (counter + 0) * period * 1000,
                           (counter + 1) * period * 1000,
             new TokenCode(getHOTP(counter + 1),
                           (counter + 1) * period * 1000,
                           (counter + 2) * period * 1000));
  }

  return null;
}

public TokenType getType() {
  return type;
}

public Uri toUri() {
  String issuerLabel = !issuerExt.equals("") ? issuerExt + ":" + label : label;

  Uri.Builder builder = new Uri.Builder().scheme("otpauth").path(issuerLabel)
          .appendQueryParameter("secret", Base32String.encode(secret))
          .appendQueryParameter("issuer", issuerInt == null ? issuerExt : issuerInt)
          .appendQueryParameter("algorithm", algo)
          .appendQueryParameter("digits", Integer.toString(digits))
          .appendQueryParameter("period", Integer.toString(period));

  switch (type) {
  case HOTP:
      builder.authority("hotp");
      builder.appendQueryParameter("counter", Long.toString(counter + 1));
      break;
  case TOTP:
      builder.authority("totp");
      break;
  }

  return builder.build();
}

@Override
public String toString() {
  return toUri().toString();
}

/**
* delete image, which is attached to the token from storage
*/
public void deleteImage() {
  Uri imageUri = getImage();
  if (imageUri != null) {
      File image = new File(imageUri.getPath());
      if (image.exists())
          image.delete();
  }
}

public void setImage(Uri image) {
  //delete old token image, before assigning the new one
  deleteImage();

  imageAlt = null;
  if (image == null)
      return;

  if (this.image == null || !Uri.parse(this.image).equals(image))
      imageAlt = image.toString();
}

public Uri getImage() {
  if (imageAlt != null)
      return Uri.parse(imageAlt);

  if (image != null)
      return Uri.parse(image);

  return null;
}
}

问题是App在每个throw new TokenUriInvalidException();语句中都崩溃了。

我尝试了许多解决方案:

1-使用警报消息或吐司(非活动类别中不允许)2-错误句柄(在throw new TokenUriInvalidException();声明后不能使用)3-将令牌类别更改为活动(运行时错误)

我希望找到解决这个难题的任何帮助或解决方案。

谢谢

展开
收起
几许相思几点泪 2019-12-03 16:17:24 687 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载