Thursday, August 13, 2015

Java Serializable Objects Usage

In order to break down Class object into bytes steam, Java.Io.Serializable is used. Whenever you want to send data over network, make sure to check if your class is implementing Serializable or not. Some points to remember to get rid of Serializable related errors.

  • If there is any inner class, it must be declared Serializable.
  • If there is any member variable of class which you don't want to send (or make serializable), declare it TRANSIENT.
  • If there is any arraylist declard e.g. Obseravable List from FX Collections, then remember that this type of list is not serializable and it will create errors. Use Simple Array List instead of FX Collections Observable List.


Example :

public class Patent implements java.io.Serializable 
{
   public String name;
   public String address;
   public transient int CNIC;
   public int number;

}

Friday, August 7, 2015

JavaFx - Validating Numeric Only Text Field

Earlier, I posted about validating IP in a Text Field. Thought of sharing information about Numeric Only validation too. So, its pretty simple. Here are some lines of codes

1. @FXML
    private TextField port;

2.  if (!validatePort(port.getText())) {
                showDialog("Invalid Port");
            }

3.  public boolean validatePort(final String port) {
        Pattern pattern;
        Matcher matcher;
        String PORT_PATTERN
                = "^([0-9]+)$";
        pattern = Pattern.compile(PORT_PATTERN);
        matcher = pattern.matcher(port);
        return matcher.matches();
    }

JavaFx - Validating Text Field with IP

Recently, I happen to come across IP validation problem in JavaFX code. So, I googled and googled. In the end, I found the relevant information. Here are some lines of codes, which is useful. Use it when you need it.

1. @FXML
    private TextField ip;

2.  if (!validateIP(ip.getText())) {
                showDialog("Invalid IP Address");
            }

3.  public boolean validateIP(final String ip) {
        Pattern pattern;
        Matcher matcher;
        String IPADDRESS_PATTERN
                = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        pattern = Pattern.compile(IPADDRESS_PATTERN);
        matcher = pattern.matcher(ip);
        return matcher.matches();
    }

Monday, January 5, 2015

Disable Camera on Android Devices

I have been going through forums and blogs where developers queried about disabling camera on android devices. Its pretty much easy. Here is how.

You need to use Device Admin privileges to have the rights to disable the camera. Remember that once an app is registered as Device Admin, it can only be uninstalled if the permissions from Device Admin (Settings section on your android device) is given.

1. Make a class (e.g. DevicePolicyActivity) and extend Activity
1.1 . Make another class  (e.g. AndroidDeviceAdminReceiver) and extend DeviceAdminReceiver

2. Make two variables like...

    DevicePolicyManager devicePolicyManager;
    ComponentName demoDeviceAdmin;

3. Initialize the variables above in onCREATE function like....
           devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

        demoDeviceAdmin = new ComponentName(this, AndroidDeviceAdminReceiver.class);

4. Now, add disable camera function against your button press or checkbox function as...

      devicePolicyManager.setCameraDisabled(demoDeviceAdmin, false);

5. Now, add Intent to your class like...

   private void activateDevicePolicyAdmin() {
        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                demoDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "Organization Policy ");
        startActivityForResult(intent, ACTIVATION_REQUEST);
    }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ACTIVATION_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i(TAG, "Administration enabled!");
                } else {
                    Log.i(TAG, "Administration enable FAILED!");
                }
                return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

6. Put this in your Android Manifest file (if using Netbeans, its under Important Files section)

        <receiver
            android:name=".AndroidDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

7. The XML file (device_admin.xml) must mention the privleges that you want to use...

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-policies>
               <disable-camera />
        </uses-policies>
</device-admin>


Here is your app for Disabling Camera in 7 easy steps. If you fail to understand any part, m round the corner!