jordan33h
4/13/2018 - 7:37 PM

[XMLess] XML less concept #xmless #android #java #function #helper #microfunction #microhelper #whaatever

[XMLess] XML less concept #xmless #android #java #function #helper #microfunction #microhelper #whaatever

public class XMLessFragment extends Fragment
{
}
public class XMLessActivity extends AppCompatActivity
{
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.File;

public class PDFViewer extends AppCompatActivity
{
	public static final String FROM        = "PDF.From";
	public static final String FROM_FILE   = "PDF.File";
	public static final String FROM_BYTES  = "PDF.Bytes";
	public static final String FROM_URI    = "PDF.Uri";
	public static final String FROM_ASSETS = "PDF.Assets";

	@Override protected void onCreate (@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		LinearLayout              llRoot   = new LinearLayout(this);
		ViewGroup.LayoutParams    prmRoot  = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		PDFView                   pdfView  = new PDFView(getApplicationContext(), null);
		LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		setContentView(llRoot, prmRoot);
		pdfView.setLayoutParams(llParams);
		llRoot.addView(pdfView);

		switch (getIntent().getStringExtra(FROM))
		{
			case FROM_FILE:
				pdfView.fromFile((File) getIntent().getSerializableExtra(FROM_FILE));
				break;
			case FROM_BYTES:
				pdfView.fromBytes(getIntent().getByteArrayExtra(FROM_BYTES));
				break;
			case FROM_URI:
				pdfView.fromUri(getIntent().getParcelableExtra(FROM_URI));
				break;
			case FROM_ASSETS:
				pdfView.fromAsset(getIntent().getStringExtra(FROM_ASSETS));
				break;
			default:
				finish();
				Toast.makeText(this, "No Supported PDF Source", Toast.LENGTH_SHORT).show();
				break;
		}
	}
    
    public static void start(Activity a, String where, Object object)
	{
		Intent i = new Intent(a, PDFViewer.class);
		i.putExtra(FROM, where);
		switch (where)
		{
			case FROM_FILE:
				i.putExtra(where, (File) object);
				break;
			case FROM_BYTES:
				i.putExtra(where, (byte[]) object);
				break;
			case FROM_URI:
				i.putExtra(where, (Uri) object);
				break;
			case FROM_ASSETS:
				i.putExtra(where, (String) object);
				break;
			default:
				Toast.makeText(a, "No Supported PDF Source", Toast.LENGTH_SHORT).show();
				return;
		}

		a.startActivity(i);
	}
}