Skip to content

Instantly share code, notes, and snippets.

@Heilum
Heilum / gist:675de27e6ae59fd0bf20c49b87e72ed3
Last active October 13, 2017 02:20
Use AES/RSA encryption algorithm to make a KeyChain on Android
package com.swordfishsoft.security;
import android.content.Context;
import android.content.SharedPreferences;
import android.security.KeyPairGeneratorSpec;
import android.security.keystore.KeyProperties;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayInputStream;
@Heilum
Heilum / gist:f5cc44bf663f3722bd19097be47ccf9b
Last active February 19, 2024 07:55
let Android Activity's transition animation like iOS navigationController's push-pop one
1.Override CommonActivity's startActivity and finish
@Override
public void startActivity(Intent intent) {
super.startActivity(intent);
overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}
@Override
public void finish() {
super.finish();
@Heilum
Heilum / Test.java
Last active June 2, 2023 09:51
Apache Tika + Tesseract-OCR to scan Chinese text in pdf
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
//
// UIViewController+SF.swift
// BowenEdu
//
// Created by CHENWANFEI on 2018/8/21.
// Copyright © 2018 swordfish. All rights reserved.
//
import UIKit
extension UIViewController{
@Heilum
Heilum / gist:2ec98991d16e7ce6a63b3207764e618b
Created August 17, 2019 11:59
How to use NSMetadataQuery in command line
let query = NSMetadataQuery();
query.searchScopes = ["/work/books"]
query.predicate = NSPredicate(format: "kMDItemFSName like %@", "NSHipster - Cocoa & Swift.pdf")
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: nil) { (_) in
print("count = \(query.resultCount)")
if let items = query.results as? [NSMetadataItem]{
for item in items{
if let path = item.value(forKey: NSMetadataItemPathKey) as? String{
print(path)
}
@Heilum
Heilum / gist:c84a9cea868cfa774cddf4a3423cf64a
Last active November 1, 2019 02:50
Setting associated userObject to UIView
//
// UIView+SF.swift
// WechatTail
//
// Created by CHENWANFEI on 2019/9/22.
// Copyright © 2019 SwordFish. All rights reserved.
//
import UIKit
extension UIView {
func findDescendantViewsOfType<T: Any>(_ subViewType:T.Type) -> [T]{
var rs = [T]()
if self is T {
rs.append(self as! T)
}
for view in self.subviews {
rs.append(contentsOf: view.findDescendantViewsOfType(T.self))
}
let targetWidth = self.view.bounds.size.width;
let targetSize = CGSize(width: targetWidth, height: UIView.layoutFittingCompressedSize.height)
//如果headView中有通过aspect ratio决定高度的constraint,这将导致systemLayoutSizeFittingSize计算错误,所以在代码中把这种constraint转为固定高度的constraint
//see : https://stackoverflow.com/questions/44801492/systemlayoutsizefitting-ignoring-layout-constraints-and-using-image-view-intrins
let imageView = self.empyHeadView.subviews.last!
imageView.constraintWithIdentifier("heightConstraint")!.constant = (targetWidth - 40) * CGFloat(0.3);
//宽度固定,高度可变
let resultSize = self.empyHeadView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.defaultHigh, verticalFittingPriority:UILayoutPriority.fittingSizeLevel );
//set
import UIKit
extension UIRefreshControl {
func beginRefreshingManually(_ tintColor:UIColor) {
self.tintColor = tintColor
if let scrollView = superview as? UIScrollView {
scrollView.setContentOffset(CGPoint(x: 0, y:scrollView.contentOffset.y - frame.height), animated: false)
}
beginRefreshing()
self.sendActions(for: .valueChanged)
//
// Collection+SF.swift
// PanoAI
//
// Created by Forever Positive on 2021/1/30.
//
import Foundation
extension Array where Element == Optional<Any>{