Android Forcing an App Chooser

MainActivity.java

package iterator.ir.appc;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(buttonShow_OnClickListener);
    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent("ir.iterator.AppA.MessageActivity");

            Intent chooser=Intent.createChooser(intent,"APP Selector");

            startActivity(chooser);
        }
    };
}

References
https://github.com/mhdr/AndroidSamples/tree/master/037

Android Explicit and Implicit Intent

AppA MainActivity.java

package iterator.ir.appa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(buttonShow_OnClickListener);
    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getBaseContext(),MessageActivity.class);
            startActivity(intent);
        }
    };
}

AppA AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="iterator.ir.appa">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MessageActivity">
            <intent-filter>
                <action android:name="ir.iterator.AppA.MessageActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

AppB MainActivity.java

package iterator.ir.appb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);
        buttonShow.setOnClickListener(buttonShow_OnClickListener);

    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent("ir.iterator.AppA.MessageActivity");
            startActivity(intent);
        }
    };
}

References
https://github.com/mhdr/AndroidSamples/tree/master/036

JAX-RS application without an Application subclass

build.gradle

group 'ir.iterator'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server
    compile group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.25.1'

    // https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.25'

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>

    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

HelloWorldResource.java

@Path("hello")
public class HelloWorldResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("get")
    public String sayhello() {
        return "hello";
    }
}

References
http://stackoverflow.com/questions/22994690/which-init-param-to-use-jersey-config-server-provider-packages-or-javax-ws-rs-a
https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3.pluggability.noapp

Android Add a Fragment to an Activity using XML

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="iterator.ir.a033.MainActivity"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="iterator.ir.a033.FirstFragment"
        android:id="@+id/fragment1"
        tools:layout="@layout/fragment_first" />

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="iterator.ir.a033.SecondFragment"
        android:id="@+id/fragment2"
        tools:layout="@layout/fragment_second" />

</LinearLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/033

URL rewriting solution for JSF

build.gradle

    // https://mvnrepository.com/artifact/org.ocpsoft.rewrite/rewrite-servlet
    compile group: 'org.ocpsoft.rewrite', name: 'rewrite-servlet', version: '3.4.1.Final'

    // https://mvnrepository.com/artifact/org.ocpsoft.rewrite/rewrite-config-prettyfaces
    compile group: 'org.ocpsoft.rewrite', name: 'rewrite-config-prettyfaces', version: '3.4.1.Final'

/WEB-INF/pretty-config.xml

<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">

	<url-mapping id="login">
		<pattern value="/login" />
		<view-id value="/legacy/user/login.jsp" />
	</url-mapping>

</pretty-config>

References
http://www.ocpsoft.org/prettyfaces/

Configure JSF with Gradle

build.gradle

group 'ir.iterator'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/com.sun.faces/jsf-api
    compile group: 'com.sun.faces', name: 'jsf-api', version: '2.2.14'

    // https://mvnrepository.com/artifact/com.sun.faces/jsf-impl
    compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.2.14'

    // https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api
    compile group: 'javax.servlet.jsp.jstl', name: 'javax.servlet.jsp.jstl-api', version: '1.2.1'

    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
            id="WebApp_ID" version="2.5">  
      <!-- Change to "Production" when you are ready to deploy -->  
      <context-param>  
           <param-name>javax.faces.PROJECT_STAGE</param-name>  
           <param-value>Development</param-value>  
      </context-param>  
      <servlet>  
           <servlet-name>Faces Servlet</servlet-name>  
           <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  
           <load-on-startup>1</load-on-startup>  
      </servlet>  
      <!-- Map these files with JSF -->  
      <servlet-mapping>  
           <servlet-name>Faces Servlet</servlet-name>  
           <url-pattern>/faces/*</url-pattern>  
      </servlet-mapping>  
      <servlet-mapping>  
           <servlet-name>Faces Servlet</servlet-name>  
           <url-pattern>*.jsf</url-pattern>  
      </servlet-mapping>  
      <servlet-mapping>  
           <servlet-name>Faces Servlet</servlet-name>  
           <url-pattern>*.faces</url-pattern>  
      </servlet-mapping>  
      <servlet-mapping>  
           <servlet-name>Faces Servlet</servlet-name>  
           <url-pattern>*.xhtml</url-pattern>  
      </servlet-mapping>  
      <!-- Welcome page -->  
      <welcome-file-list>  
           <welcome-file>welcome.xhtml</welcome-file>  
      </welcome-file-list>  
 </web-app>  

References
http://b1102.blogspot.ru/2014/09/jsf-21-gralde-tomcat-hello-world.html

Enable linux swap partition

You have no configuration for swap in /etc/fstab . Add following line to that file:

UUID=<uuid> none   swap    sw    0       0   

You have to replace with the uuid of your swap partition. To do that, run sudo blkid

$ sudo blkid
/dev/sda1: LABEL="System Reserved" UUID="88A0D0A1A0D09752" TYPE="ntfs" 
/dev/sda2: UUID="0620D9F920D9EFA3" TYPE="ntfs" 
/dev/sda5: UUID="c282b418-2045-4852-8789-88a44360a0bb" TYPE="ext4" 
/dev/sda6: UUID="f99c6a0c-790a-45ca-a1a9-8874f5a2999b" TYPE="ext4" 
/dev/sda7: UUID="4cc2e909-ebd1-4c72-abee-aa32035bf330" TYPE="swap"

References
http://askubuntu.com/questions/194775/swap-not-available-i-must-manually-swapon-after-every-reboot

Android Communication between two Fragments

FirstFragment.java

public class FirstFragment extends Fragment {

    Button buttonSend;
    EditText editTextName;

    private OnFragment1InteractionListener mListener;

    public FirstFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_first, container, false);

        buttonSend= (Button) view.findViewById(R.id.buttonSend);
        editTextName= (EditText) view.findViewById(R.id.editTextName);

        buttonSend.setOnClickListener(onClickListener);

        return view;
    }

    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name=editTextName.getText().toString();
            onButtonPressed(name);
        }
    };

    public void onButtonPressed(String name) {
        if (mListener != null) {
            mListener.onFragment1ButtonClicked(name);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragment1InteractionListener) {
            mListener = (OnFragment1InteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragment1InteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragment1InteractionListener {
        void onFragment1ButtonClicked(String name);
    }
}

SecondFragment.java

public class SecondFragment extends Fragment {

    TextView textViewMessage;

    public SecondFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View  view= inflater.inflate(R.layout.fragment_second, container, false);

        textViewMessage= (TextView) view.findViewById(R.id.textViewMessage);

        return view;
    }

    public void setTextViewMessage(String name)
    {
        textViewMessage.setText("Hello " + name);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements FirstFragment.OnFragment1InteractionListener{

    RelativeLayout container1;
    RelativeLayout container2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        container1= (RelativeLayout) findViewById(R.id.relativeLayoutContainer1);
        container2= (RelativeLayout) findViewById(R.id.relativeLayoutContainer2);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


        FirstFragment firstFragment=new FirstFragment();
        SecondFragment secondFragment=new SecondFragment();

        transaction.add(R.id.relativeLayoutContainer1,firstFragment);
        transaction.add(R.id.relativeLayoutContainer2,secondFragment);

        transaction.commit();
    }

    @Override
    public void onFragment1ButtonClicked(String name) {
        SecondFragment fragment= (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.relativeLayoutContainer2);
        fragment.setTextViewMessage(name);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/032