Skip to content

Instantly share code, notes, and snippets.

View rayworks's full-sized avatar
:atom:

rayworks rayworks

:atom:
  • Shanghai, China
  • 17:49 (UTC +08:00)
View GitHub Profile
@rayworks
rayworks / Utils.java
Last active June 8, 2016 04:04
Read lines from file in folder asserts
public static List<String> readLinesFromAssetFile(String fileName, final Context context) throws IOException {
InputStream inputStream = context.getAssets().open(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
LineReader lr = new LineReader(br);// from Guava
List<String> lines = new LinkedList<>();
String line;
while ((line = lr.readLine()) != null){
lines.add(line);
private void doImport() {
Uri fullContacts = Uri.parse("content://com.android.contacts/raw_contacts");
Uri itemContact = Uri.parse("content://com.android.contacts/data");
int importCnt = 0;
try {
InputStream inputStream = getAssets().open("ct2.txt");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
@rayworks
rayworks / BlurEffect
Last active April 12, 2017 07:49
Scale the image based on the target view's dimension while keeping its ratio, and then make the cropped part blurred.
private void refreshAvatar(File file) {
/***
* Note: the current Fragment has been created once entering the home Activity.
* So the dimension of avatarParent is determinate already.
*/
if (file != null && file.exists()) {
avatarParent.setBackground(defaultBackground);
Glide.with(this).load(file.getAbsoluteFile())
.asBitmap()
@rayworks
rayworks / gist:d257306eb5f6d6b14a752d217984b107
Created November 24, 2017 09:51
Reset the view when applying MVP pattern
/**
* Returns the view configured in the presenter which real implementation is an Activity or
* Fragment using this presenter.
*/
public final T getView() {
return view;
}
/**
* Configures the View instance used in this presenter as view.
@rayworks
rayworks / PlaceHolderFragment
Created February 26, 2018 09:04
Get rid of IllegalStateException when comitting the fragment transaction after the activity’s state had been saved
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import java.util.LinkedList;
import timber.log.Timber;
@rayworks
rayworks / downloader
Created April 13, 2018 09:49
A simple cancellable downloader
import okhttp3.ResponseBody;
import retrofit2.Call;
public interface Repository {
Call<ResponseBody> download(String url);
}
public void cancelCurrentDownloadingTask() {
if (call != null) {
call.cancel();
@rayworks
rayworks / gist:bd4cd60a728ae761baf81188285ae459
Created April 18, 2018 08:38
Multiple apks generated with different channel ids via Jenkins scripts
next=`cat /jenkins-jobs/your-job/nextBuildNumber`
one='1'
no=`echo $((next - one))`
rc=`ls /jenkins-jobs/your-job/builds/${no}/archive/app/build/outputs/apk/ | grep "app-live-release"`
java -jar /packer-tool-path/packer-ng-plugin/tools/packer-ng-2.0.0.jar generate --channels=google \
--output=/jenkins-jobs/your-job/builds/${no}/archive/app/build/outputs/apk/channel/ \
/jenkins-jobs/your-job/builds/${no}/archive/app/build/outputs/apk/${rc}
grep -R --include="*.json" "YOUR-KEYWORDS\":" ./ | cut -d':' -f1 | sort | uniq
@rayworks
rayworks / ViewUtils
Last active May 22, 2018 08:26
Using scaleType 'Matrix' to implement 'center_crop' or 'left_top aligned crop' effect
public static void setImageMatrixWithRatioKept(
ImageView view, Bitmap resource, boolean scaleByActualWidth, boolean alignmentCenter) {
view.setScaleType(ImageView.ScaleType.MATRIX);
int bmpWidth = resource.getWidth();
int bmpHeight = resource.getHeight();
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
float scaleWidth = 1.0f * viewWidth / bmpWidth;
@rayworks
rayworks / gist:428409347a4e1c69ab764da1b60856c8
Created February 2, 2019 05:46
Commands for screen recording and media convertion
adb shell screenrecord --time-limit 3 --bit-rate 2000000 --verbose /sdcard/demo.mp4
ffmpeg -ss 00:00:00.000 -i ./demo.mp4 -pix_fmt rgb24 -r 3 -s 270x480 -t 00:00:3.000 output.gif