Yet Another Blog

April 22, 2015

Keyboard bug in Intellij under Ubuntu

Filed under: computer, Java, linux — Tags: , , , , , , — guilleml @ 3:09 pm

If you’re using intellij under Ubuntu you probably noticed the keyboard stops working sometimes just in the IDE.
This is very anoying and there is an open issue: https://youtrack.jetbrains.com/issue/IDEA-78860
If you want to enable again the keyboard execute this command on any terminal:

ibus-daemon -rd

This seems to work in versions 14.04 and 14.10.

Source: http://askubuntu.com/questions/501907/keyboard-locks-in-intellij-idea-on-ubuntu-14-04

Advertisements

February 17, 2015

Setting environment variables in OpenShift

Filed under: computer, Java, linux, Software — Tags: , , , , — guilleml @ 12:51 pm

If you need to set some environment variables in your GEAR you can use an action hook.
The pre-start action hook will serve you well but if you need to restore those variables after a gear restart, pre-start action hook won’t work.
Post-restart action hook, on the other hand, will execute its actions but I haven’t managed to get the environment variables working. After its execution all environment variables that should have a value were empty.

What I did was to modify pre-start action hook to create environment variables as files under $HOME/.env/user_vars

# Actual script
export OPENSHIFT_POSTGRESQL_DB_HOST="xxx.xxx.xxx.xxx"
export OPENSHIFT_POSTGRESQL_DB_PORT="***"
export OPENSHIFT_POSTGRESQL_DB_NAME="***"
export OPENSHIFT_POSTGRESQL_DB_USERNAME="***""

# Added script for post restart variables
echo "xxx.xxx.xxx.xxx" > OPENSHIFT_POSTGRESQL_DB_HOST
echo "***" > OPENSHIFT_POSTGRESQL_DB_PORT
echo "***" > OPENSHIFT_POSTGRESQL_DB_USERNAME
echo "***" > OPENSHIFT_POSTGRESQL_DB_PASSWORD

After this, if you execute gear restart, the environment variables will exist and will be accesible from your application.

January 21, 2015

Asking for root in your Android app

Filed under: Android, computer, Java, Software — Tags: , , , , , — guilleml @ 12:34 pm

If your app needs root permissions to execute any command, you can do this using something like:


Process p;
try {
   p = Runtime.getRuntime().exec("su");
   DataOutputStream os = getRootSession(p);
   os.writeBytes("any command you want\n");
   os.flush();
} catch (IOException e1) {
   Log.d(MainActivity.class.getName(), "Error: " + e1.getMessage());
}

After running Runtime.getRuntime().exec(“su”) a dialog will appear to ask for permissions so the user can accept it.
What happens if you’re writing a service that will need root access anytime, even if the screen is off? The user won’t be able to accept the dialog so the app will fail.

To solve this you can do your app to ask for root permissions when it runs so, if the user accepts forever, the app will be able to run normally without asking the user anytime.
I know it’s better the user knows when an app is doing something as root but there are cases where you need to do this, for example, I’m writing an app that will disable charging for a phone when
the battery is charget at 100% in order to avoid microcharges. If you let the phone charging at night you won’t be there to disconnect the phone when it gets to 100% so this app monitors battery charge level and will use root permissions when the battery is 100%.

What I’ve done is to create an initial activiy to ask for permissions:

AskingRootActivity.java

public class AskingRootActivity extends ActionBarActivity {
private static final String TAG = AskingRootActivity.class.getName();

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_asking_root);
}
private void nextScreen(){
   Intent intent = new Intent(getApplicationContext(), NextActivity.class);
   startActivity(intent);
}

public void checkRoot(View v) {
   Process p;
   try {
      p = Runtime.getRuntime().exec("su");
      DataOutputStream os = new DataOutputStream(p.getOutputStream());
      os.writeBytes("ls /data\n");
      os.writeBytes("exit\n");
      os.flush();
      try {
         p.waitFor();
         if (p.exitValue() != 1) {/
         nextScreen();
         Log.d(TAG, "success getting root");
      }
      else {
         TextView tv = (TextView) findViewById(R.id.actionMsg);
         tv.setText(R.string.root_error);
         Log.d(TAG, "failing getting root");
      }
   } catch (InterruptedException e) {
      Log.d(TAG, "failing getting root");
}
} catch (IOException e) {
   Log.d(TAG, "failing getting root");
}
}
}

When the user clicks on the button, the app will try to get a root session so a dialog asking for root will appear.

After the user accepts or denies it, we try to do something we can only do as root, like listing files in /data directory. If the command runs succesfully, we navigate to the next screen/activity/logic/whatever, if not, we can show an error or finish the app.
As the app won’t do anything untill the user accepts or denies it, we can be sure when the app gets to the point when it needs root access, it will have it, at least if the user didn’t set a timeout for the permission.

activity_asking_root.xml

<RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot; android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
    android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
    android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
    android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
    tools:context=&quot;es.guille.sample.root.activities.AskingRootActivity&quot;>

    <TextView
        android:id=&quot;@+id/actionMsg&quot;
        android:text=&quot;@string/asking_root&quot; 
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; />

    <Button
        android:id=&quot;@+id/nextbtn&quot;
        android:onClick=&quot;checkRoot&quot;
        android:text=&quot;@string/ask_for_root&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/actionMsg&quot;/>

</RelativeLayout>

This layout only defines an info message and a button to ask for root permissions.

May 26, 2011

Increasing Debugging Eclipse PermSize while developing plugins

Filed under: computer, Java, Software — Tags: , , , , — guilleml @ 8:57 pm

The configuration is quite simple, I just got tired of permgens errors while developing a plugin in Eclipse.
This is how I’m launching and debugging a plugin.

Blog at WordPress.com.