Create fullscreen Activity in Android

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

AndroidManifest.xml

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.AppCompat.Light.NoActionBar"/>

References
https://developer.android.com/training/system-ui/index.html
https://stackoverflow.com/questions/2868047/fullscreen-activity-in-android

Pre Caching LayoutManager for RecyclerView

PreCachingLayoutManager.java

public class PreCachingLayoutManager extends LinearLayoutManager {
    private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600;
    private int extraLayoutSpace = -1;
    private Context context;
 
    public PreCachingLayoutManager(Context context) {
        super(context);
        this.context = context;
    }
 
    public PreCachingLayoutManager(Context context, int extraLayoutSpace) {
        super(context);
        this.context = context;
        this.extraLayoutSpace = extraLayoutSpace;
    }
 
    public PreCachingLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        this.context = context;
    }
 
    public void setExtraLayoutSpace(int extraLayoutSpace) {
        this.extraLayoutSpace = extraLayoutSpace;
    }
 
    @Override
    protected int getExtraLayoutSpace(RecyclerView.State state) {
        if (extraLayoutSpace > 0) {
            return extraLayoutSpace;
        }
        return DEFAULT_EXTRA_LAYOUT_SPACE;
    }
}

References
https://androiddevx.wordpress.com/2014/12/05/recycler-view-pre-cache-views/

Working with Glide, Image Loader Library for Android

build.gradle

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:4.0.0'
  compile 'com.android.support:support-v4:25.3.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
}

ProGaurd

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

Generated API

MyAppGlideModule.java

package com.example.myapp;
   
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
   
@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}

Kotlin

apply plugin: 'kotlin-kapt'
kapt 'com.github.bumptech.glide:compiler:4.0.0'

Use :

            GlideApp.with(context)
                    .load(image.getFile())
                    .override(measuredWidth,measuredWidth)
                    .centerCrop()
                    .placeholder(context.getResources().getDrawable(R.mipmap.image_placeholder))
                    .into(holder.imageViewThumbnail);

References
http://bumptech.github.io/glide/
https://github.com/bumptech/glide
http://bumptech.github.io/glide/doc/generatedapi.html
https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Android Requesting Permissions at Run Time

public class ThumbnailFragment extends Fragment {

    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1000;
    private List<ImageGroup> imageGroupList;
    private RecyclerView recyclerViewImages;
    private ThumbnailPageAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    public ThumbnailFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_thumbnail, container, false);

        layoutManager = new LinearLayoutManager(getContext());

        adapter = new ThumbnailPageAdapter();

        recyclerViewImages = (RecyclerView) view.findViewById(R.id.recyclerViewImages);
        recyclerViewImages.setLayoutManager(layoutManager);
        recyclerViewImages.setAdapter(adapter);

        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                // if we are in activity
/*                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);*/

                // if we are in fragment
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
        else {

            // if we have permission we can run our permission related task
            ListFiles();
        }

        return view;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // permission-related task you need to do.

                    // this will run just after granting permission
                    // not always

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ListFiles();
                        }
                    });

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    public void ListFiles()
    {
        final Path path = new Path();
        final Search search = new Search();
        final AddFileHelper addFileHelper = new AddFileHelper(adapter);

        search.setOnListFilesListener(new Search.OnListFilesListener() {
            @Override
            public void onNewFileFound(File file) {
                addFileHelper.addNewFile(file);
            }

            @Override
            public void onListCompleted(List<File> result) {
                //addFileHelper.close();
            }
        });


        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                search.listFiles(path.getDCIMFile());
            }
        });

        thread.start();
    }
}

References
https://developer.android.com/training/permissions/requesting.html

Android Recyclerview GridLayoutManager column spacing by ItemDecoration

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
    this.space = space;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
  }
}
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

References
https://stackoverflow.com/questions/28531996/android-recyclerview-gridlayoutmanager-column-spacing

Get MIME Type on Android

    public String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        return type;
    }

Work with BlockingQueue in Java

Unbounded Queue

BlockingQueue<String> blockingQueue = new LinkedBlockingDeque<>();

Bounded Queue

BlockingQueue<String> blockingQueue = new LinkedBlockingDeque<>(10);

Adding Elements

  • add() – returns true if insertion was successful, otherwise throws an IllegalStateException
  • put() – inserts the specified element into a queue, waiting for a free slot if necessary
  • offer() – returns true if insertion was successful, otherwise false
  • offer(E e, long timeout, TimeUnit unit) – tries to insert element into a queue and waits for an available slot within a specified timeout

Retrieving Elements

  • take() – waits for a head element of a queue and removes it. If the queue is empty, it blocks and waits for an element to become available
  • poll(long timeout, TimeUnit unit) – retrieves and removes the head of the queue, waiting up to the specified wait time if necessary for an element to become available. Returns null after a timeout

References
http://www.baeldung.com/java-blocking-queue
https://pupli.net/2017/07/24/remote-procedure-call-rpc-sample-in-rabbitmq/