Tuesday, May 24, 2016

Getting MotoG active display without Root

Hey guys, I am starting a new series of introducing some of the very cool apps of Android.

Today I am going to review an app that will emulate MotoG Active Display feature. The app is ACDisplay.

Here are steps to install ACDisplay and make it look alike Active Display.
  1. Install it using the play store, link.
  2. After installation open it.

  3. Clicking the yellow mark will tell you what all permissions you have to give AcDisplay.


  4. Give all the permissions to it, namely Device Administrators and Notification Access.

      
  5. Device Administrators is required to lock the Device and Notification Access is required so that you can read all the notifications(although you can configure it, which Apps you don't need).
  6. You are almost done (yellow mark is also gone), after you give the permissions your basic setup is ready to use.
  7. Enable the global button on the top right hand corner.

  8. This will enable the app. Now lets change some config. 
  9. Settings will have 2 sections:
    1. Features
      1. LockScreen
        1. Go to this and enable it on the top right hand corner, this will make this as the lock screen
        2. Check Enable when no notifications if you want to show it irrespective of whether there are 0 notifications or more than that.
      2. Active Mode
        1. Go to this and enable it on top right hand corner, this will give you options for the behavior of the AcDisplay, wave to wake, disable on low battery etc.
    2. Settings
      1. Notifications
        1. This will give you the option what to do with the notification, like wake up or not on arrrival of new notification, to show always on notifications or not etc.
      2. Interface
        1. This will give you the option to use the desktop wallpaper as background for this lock screen, animation, colors etc.
      3. Apps
        1. This will be useful to blacklisting the app, that means app which you don't want to show the notifications for.
      4. More
        1. This gives you the option when to make this app active, only on charging or everytime, inactive hours (for which app will be in sleep), shortcuts like lower left hand shortcut, lower right hand shortcut etc.
  10. After then when you lock the device and wake it up, AcDisplay will spread its magic, but the main problem is that it just disappears if you press the home button, and comes to home screen, so in order to solve that we will pattern protect our phone via default locker. 
  11. For that go to Settings->Lock Screen->Screen Lock->Pattern and give the pattern, confirm it.
  12. Enable :
    1. Directly Show pattern view, 
    2. make pattern visible
    3. show pattern error
    4. show pattern dots
  13. Set automatically lock to immediately, enable power button instantly locks
  14. At last we will disable notifications in lock screen, since our notifications will come at AcDisplay, for that Settings->Notifications->When device is locked->Don't show notifications at all
  15. Now lock the screen, and whenever notification come, depending on the wake up setting of the AcDisplay, mobile will lighten up, and show the notification very similar to the Moto Active Display, tap the small icon and read the notification, tapping after selecting the notification will take you to the app itself, but before that you have to give correct pattern.

Notes:

  1. The AcDisplay best works with default lockscreen, do not use with other lock screens.
  2. Don't forget to set the pattern or pin or password lock for default lock since pressing the menu button bypasses the AcDisplay screen.
  3. It acts as a module of Xposed as well, which is an optional setting, you can enable it in Xposed Module, modules section, if your Xposed module is setup, that means both rooted and Xposed installed and configured.

Monday, May 16, 2016

Remove duplicates from sorted array.

There are a number of approaches when it comes to remove the duplicates, from the array. Here are some of them:

  1. Make a Set out of the array so that you have only the unique elements out of it, also considering the further point if you want to keep track of the order of the elements you can use LinkedHashSet that guarantees the uniqueness as well as order of the Collection.
  2. Next approach is bit granular and we traverse the array and keeps on pushing the pointers till I am getting the same element:
    1. Start with 2 pointers first marked at 0 (since it is an array, lets name it as f) and second marked at 1(lets name it as s).
    2. Keep incrementing the second pointer till the elements at ARRAY[f] and ARRAY[s] are equal.
    3. When above condition is false make ARRAY[f] = ARRAY[s] and increment both f and s since the position of f and s are traversed.
Coming to the implementation, here is the working code.

This is the code you can find the running code here.

import java.util.*;
import java.lang.*;
import java.io.*;
 
class Ideone
{
      public static int[] duplicatesRemoval(int[] input){
 
        int f = 0;
        int s = 1;
        //RETURN IF THE ARRAY LENGTH IS LESS THAN 2;
        if(input.length < 2){
            return input;
        }
        while(s < input.length){
            if(input[s] == input[f]){
                s++;//
            }else{
             /*
             **THIS PART CAN BE SHORTENED BY input[++f] = input[s++];
             */
             f++;//SINCE LAST POSITION IS 1 BACK TO CURRENT ONE
                input[f] = input[s];
                s++;//SINCE s POSITION IS ALREADY TRAVERSED SO INCREMENT IT
            }   
        }
        //reassigning to new array element
        int[] result = new int[f+1];
        for(int k=0; k<result.length; k++){
            result[k] = input[k];
        }
 
        return result;
    }
    public static void main(String a[]){
        int[] input = {2,3,6,6,8,9,10,10,10,12,12};
        int[] output = duplicatesRemoval(input);
        for(int i:output){
            System.out.print(i+" ");
        }
    }
}

Wednesday, May 11, 2016

Next greater number with same set of digits

This is pretty interesting question I came across when facing an interview.

The question was pretty straight forward, you have given a number and you have to find the next greater number with the same set of digits as in the given number. So for an instance if Number is:
  1. 1234 then output should be 1243
  2. 4321 then output should be not possible since its the largest among all the possible numbers with the same digits as in 1234
  3. For other cases we have to develop the algorithm (:D).
Now coming to the solution of this problem, one straight forward algorithm would be:
  1. Find all the possible combination of the string (given number) and collect it in a Sortable Collection like TreeSet and take the next element after the given number. This is pretty straight forward approach but it is waste since we have to calculate the full set of numbers that are possible to get generated and then find the next number in the TreeSet. This is simple to understand but memory wise, order wise and space wise pretty heavy.
    Although I have given this answer only :D, but the interviewer was happy with it at all. Since there are better approaches to this.
  2. Next approach is pretty simple but a bit difficult to understand. Here it goes:
    1. First 2 condition will be same as the ascending and descending one.
    2. If the case is different then we will :
      1. take pair of digits from right where right digit is greater then left digit, and mark the index to be right digit index - 1.
      2. Then we have to take the smallest digit from the right hand side digits of the marked index which is greater then the indexed digit
      3. Then sort the right side of the index digit in ascending order.
      4. What, what what :D
    3. Lets take an example: 54362
      1. In this example start from the right in pair, (2,6) [we will ignore since 2<6] and move to next pair that is (6,3). This is correct pair since 6>3.
      2. So our marking index would be 2 and the number to be updated is 3.
      3. Now taking the number which appears in right side of index number(ie 2) and is smallest among them but larger then 3.
      4. In this case it is 6 only since 2 is smallest but 2>3 is false.
      5. So we swap 3 and 6 which will make the number as 54632 and sort the right hand of index(2) that means numbers 3,2 have to be sorted which is true in this case.
Lets make the program of this. Here it goes:

MyData.java
import java.util.Arrays;

/**
 * @author ankur
 *
 */
public class MyData {

 private int n;
 private int nAsc;
 private int nDesc;
 private int[] nIntArr;
 private int length;
 
 private String sAsc;
 private String sDesc;
 
 
 public MyData(int n){
  this.n = n;
  
  process();
 }
 
 private void process(){
  String s = "" + this.n;
  char [] sArr = s.toCharArray();
  
  nIntArr = new int[sArr.length];
  for(int i=0, len=sArr.length; i<len; i++){
   nIntArr[i] = Integer.parseInt("" + sArr[i]);
  }
  Arrays.sort(sArr);
  sAsc =  new String(sArr);
  sDesc = new StringBuffer(sAsc).reverse().toString();
  nAsc = Integer.parseInt(sAsc);
  nDesc = Integer.parseInt(sDesc);
  length = this.sAsc.length();
 }
 
 public int getN() {
  return n;
 }
 public void setN(int n) {
  this.n = n;
 }

 public int getnAsc() {
  return nAsc;
 }

 public void setnAsc(int nAsc) {
  this.nAsc = nAsc;
 }

 public int getnDesc() {
  return nDesc;
 }

 public void setnDesc(int nDesc) {
  this.nDesc = nDesc;
 }

 public String getsAsc() {
  return sAsc;
 }

 public void setsAsc(String sAsc) {
  this.sAsc = sAsc;
 }

 public String getsDesc() {
  return sDesc;
 }

 public void setsDesc(String sDesc) {
  this.sDesc = sDesc;
 }

 public int[] getnIntArr() {
  return nIntArr;
 }

 public void setnIntArr(int[] nIntArr) {
  this.nIntArr = nIntArr;
 }

 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }
 
 
}

Main.java
package com.av.ngnsd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

 public static void main(String[] args) {
  MyData myData = new MyData(54362);
  try{
   int nextNum = process(myData);
   System.out.println("Next number is " + nextNum);
  }catch(Exception ex){
   //ex.printStackTrace();
   System.out.println(ex.getMessage());
  }
 }
 public static int process(final MyData n) throws Exception{
  int result = 0;
  int nDesc = n.getnDesc();
  int nAsc = n.getnAsc();
  if(nDesc == n.getN()){
   throw new Exception("Not possible");
  }else if(nAsc == n.getN()){
   String s = "" + nAsc;
   result = Integer.parseInt(s.substring(0, s.length()-2) + "" + s.charAt(s.length()-1) + s.charAt(s.length()-2));  
  }else{
   //TODO the main code
   int []nCharArr = n.getnIntArr();
   int indexOfChange = -1;
   for(int i=n.getLength()-1; i>0; i--){
    if(nCharArr[i]>nCharArr[i-1]){
     indexOfChange = i;
     break;
    }
   }
   
   if(indexOfChange <= 0){
    throw new Exception("Not possible");
   }
   else{
    int numberToBeSwapped = nCharArr[indexOfChange-1], smallestIndex = indexOfChange;
    //getting the smallest among the right side of the index which is greater then index digit
    for(int i=indexOfChange+1; i<n.getLength(); i++){
     if(numberToBeSwapped < nCharArr[i] && nCharArr[i] < nCharArr[smallestIndex] ){
      smallestIndex = i;
     }
    }
    
    //swapping the 2 digits
    int temp = numberToBeSwapped;
    nCharArr[indexOfChange-1] = nCharArr[smallestIndex];
    nCharArr[smallestIndex] = temp;
    
    //for sorting the right hand side of the index
    List<Integer> nArrayList = new ArrayList<Integer>();
    for(int i=indexOfChange+1; i<n.getLength(); i++){
     nArrayList.add(nCharArr[i]);
    }
    
    //int indexOfChangeCopy = indexOfChange;
    Collections.sort(nArrayList);
    for(int num:nArrayList){
     nCharArr[++indexOfChange] = num;
    }
    StringBuilder strNum = new StringBuilder();
    for (int num : nCharArr) 
    {
         strNum.append(num);
    }
    result = Integer.parseInt(strNum.toString());
   }
  }
  
  return result;
 }
 
}