Skip to content

Instantly share code, notes, and snippets.

@li2
li2 / ProgressRequestBody.java
Last active March 20, 2018 11:50
[Is it possible to show progress bar when upload image via Retrofit 2?] http://stackoverflow.com/questions/33338181/is-it-possible-to-show-progress-bar-when-upload-image-via-retrofit-2/33384551#33384551 #tags: android-network
public class ProgressRequestBody extends RequestBody {
private File mFile;
private String mPath;
private UploadCallbacks mListener;
private static final int DEFAULT_BUFFER_SIZE = 2048;
public interface UploadCallbacks {
void onProgressUpdate(int percentage);
void onError();
@li2
li2 / IOHelper.java
Last active March 20, 2018 07:05
read bytes stream from IO #tags: java
class IOHelper {
public byte[] readBytes(InputStream inputStream) throws IOException {
// this dynamically extends to take the bytes you read
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// this is storage overwritten on each iteration with bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
// we need to know how may bytes were read to write them to the byteBuffer
@li2
li2 / dpToPixel.java
Last active March 20, 2018 05:08
dp to pixel #tags: android-view
/**
* Convert the dps to pixels, based on density scale
* @param dp value expressed in dps
* @return value expressed in pixels
*/
public int dpToPixel(int dp) {
// Get the screen's density scale
float scale = getResources().getDisplayMetrics().density;
// Add 0.5f to round the figure up to the nearest whole number
return (int) (dp * scale + 0.5f);
/**
* Created by weiyi.li on 4/7/16.
* This activity includes {@link AlbumFragment} and {@link ThumbnailFragment}.
*/
public class TwoFragmentsActivity extends BasicOperationActivity {
private boolean mShowingThumbnail = true;
@Dvr.DvrFolder private int mFolderType = Dvr.DVR_FOLDER_LOOPED_VIDEOS;
private FragmentManager mFm;
private AlbumFragment mAlbumFragment;
private ThumbnailFragment mThumbnailFragment;
/**
* Created by weiyi on 2/23/16.
* http://li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html
*/
public class SingleFragmentActivtiy extends BasicActivity {
@Override
protected String getActionBarTitle() {
return "Single Fragment Activity";
}
/**
* Created by weiyi.li on 2/19/16.
* http://li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html
*/
public abstract class BasicOperationActivity extends BasicActivity {
private ToggleButton mOperationBtn1;
private ToggleButton mOperationBtn2;
/** Override this method to inflate bottom toolbar layout. */
@Override
/**
* Created by weiyi.li on 2/19/16
* li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html
*/
public abstract class BasicActivity extends AppCompatActivity {
protected TextView mActionTitleView;
protected TextView mActionBackBtn;
protected Button mActionCloseBtn;
protected Fragment mContentFragment;
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/topActionbarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:elevation="@dimen/elevation_header">
<android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/toolbar_actionbar"
@li2
li2 / BasicServiceActivity.java
Last active March 20, 2018 06:18
A basic abstract activity to bind & unbind service, its subclasses just override onServiceAttached(Service service) method to get the refercence of service. #tags: android-service
// https://stackoverflow.com/a/38847374/2722270
public abstract class BasicServiceActivity extends AppCompatActivity {
protected DvrService mDvrService;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic);
attachService();