Thursday, June 25, 2015

Java: Unsupported major.minor version 51.0

When you get an error:

Unsupported major.minor version X

This means that you are trying to compile your Java program with version lower that the code is written for; for example you develop with Java 1.7, deploy on the server where they have Java 1.6:

Unsupported major.minor version 51.0

The fix is:

  • to change your code to use lower Java, which is sometimes impossible because of 3rd party library that you are using
  • upgrade the other computer to modern Java, which is sometimes impossible because of a brick-head gatekeeper
  • vent on the blog and start cutting out the code 


Java versions:
  • J2SE 8 = 52
  • J2SE 7 = 51 
  • J2SE 6.0 = 50 
  • J2SE 5.0 = 49 
  • JDK 1.4 = 48 
  • JDK 1.3 = 47 
  • JDK 1.2 = 46 
  • JDK 1.1 = 45

Monday, June 22, 2015

RoboGuice - Android development made simple and fun



"Simple and Fun!" Now, they make quite a promise!

read up:

https://github.com/roboguice/roboguice/blob/master/README.md

https://github.com/roboguice/roboguice/wiki

Maven repo:


org.roboguice
roboguice
3.0.1


OK, so most of all RoboGuice is about DEPENDENCY INJECTION



Examples:


@InjectView(R.id.label)
TextView label;

@InjectResource(R.string.app_name) // strings, drawables
String applicationName;

@Inject // system service
LayoutInflater layoutInflater;

@Inject
LocationManager locationManager;

@Inject // POJO
Book book;


Creating Activities



public class FightForcesOfEvilActivity extends RoboActivity {

@InjectView(R.id.expletive) 
TextView expletiveText;

Creating Android Modules


public class MyModule extends AbstractModule {
@Override
protected void configure() { }



AsynchTask 


public static class AsyncPunch extends RoboAsyncTask {

// Astroboy is a @Singleton public class Astroboy {
@Inject Astroboy astroboy;

// new instance of java.util.Random, since we haven't specified any binding instructions
@Inject Random random;


SHH key

$ git clone git@github.com:roboguice/roboguice.git
Cloning into 'roboguice'...

Permission denied (publickey).


$ ls -al ~/.ssh
total 120
-rw-------   1 uki  staff   1675 Oct 31  2012 id_rsa


COPY YOUR PUBLIC KEY to clipboard

$ pbcopy < ~/.ssh/id_rsa.pub 


PASTE your SSH key exactly as copied (i.e. in GitHub settings)
https://github.com/settings/ssh


$ git clone git@github.com:roboguice/roboguice.git roboguice
Cloning into 'roboguice'...
remote: Counting objects: 18463, done.

Receiving objects:  15%

Tuesday, June 16, 2015

MacBook Pro (late 2011) graphics crashing

With the update to Yosemite (maybe unrelated) my Mac started to crash, at first it was rare, but right now it is consistent, the common situations when it is crashing:


  • connecting external monitor via HDMI
  • connecting external monitor via VGA
  • screen sharing using JoinMe 
  • bad days in general
The MacBook Pro (starting with early 2011) have 2 GPU (graphic cards), one integrated, low power, and one additional (discrete) that is used for high power situations.

I installed the utility called gfxCardStatus that allows me to switch between integrated and discrete graphic cards.

From their website:

gfxCardStatus v2.3 and above actively prevents you from switching to Integrated Only mode when any apps are in the Dependencies list (or if you have an external display plugged in). This is because if you were to do this, your discrete GPU would actually stay powered on, even though you've switched to the integrated GPU.

So I guess no more external display for me until Apple fixes the problem, or I but new MacBook Pro (hopefully with touch screen this time).




https://developer.apple.com/library/mac/qa/qa1734/_index.html



Reseting System Management Controller (SMC)

https://support.apple.com/en-us/HT201295

Thursday, June 11, 2015

Java: Closable

When working with multiple resources that need to be closed after we are done using, I particularly like the approach of using Closable Interface:


try {
    // open input streams and use them  
}
catch (IOException e) {
    e.printStackTrace();
}
finally {
    attemptClose(inputSteamA);
    attemptClose(inputSteamB);
}



private void attemptClose(Closeable object) {
    if (object != null) {
        try {
            object.close();
        }
        catch (IOException ignore) {
        }
    }
}




Apple Photos opening every time phone USB is connected

I work developing Android so I plug and unplug different devices all day long. Apple Mac Photos app is opening every time phone USB is connected, which is frustrating and sometimes out of place at work.

To turn off that behavior next time the Photos app opens UNSELECT THIS CHECKBOX:

"Open Photos for this device"


Android: TextView in ScrollView continous scrolling

Sometimes when continuously outputting text to TextView, you would like it to scroll down:



textView.addTextChangedListener(new TextWatcher() {
    @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        // scroll down whenever log text updated to display latest info on the screen        
        
        scrollView.post(new Runnable() {
            @Override public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }

    @Override public void afterTextChanged(Editable s) {
    }
});

Monday, June 8, 2015

Android: AsyncTask delegate

Let say we have an Activity that wants to receive a messages from AsyncTask.


We create an Interface AsyncResponse

public interface AsyncResponse {
   void publishLog(String logContent);
}

We create our AsyncTask:

public class FileReaderTask 
 extends AsyncTask < Void, Void, List < AbstractX > > {
// delegate should be set in the Activity   
public AsyncResponse delegate = null;
String xyz = "";

// ...

@Overrideprotected void onPreExecute() {
   delegate.publishLog(xyz);
}

@Overrideprotected void onPostExecute(List x) {
...
      delegate.publishLog(xyz);
   }
}

Now we can tie together the AsyncTask and Activity:

public class FileReaderActivity extends Activity implements AsyncResponse {

   private static final String TAG = FileReaderActivity.class.getCanonicalName();
   FileReaderTask asyncTask = new FileReaderTask();

   private TextView logText;

   /**    * Called when the activity is first created.    */  

 @Override   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      logText = (TextView) findViewById(R.id.logText);

      // tell Async Task that this Activity will listen      

asyncTask.delegate = this;
      asyncTask.execute();
   }

   /**    * This method will receive log from Async Task.    * @param logContent    */   

@Override   public void publishLog(String logContent) {
      logText.append(logContent);
   }
}


Android: finding SD Card

Since the implementation of SD card is different on various devices use following method:


private static final String SDCARDFILE 
Environment.getExternalStorageDirectory() 
+ "/somefolder/somefile.ext";

git: updating all repos using Bash

I have a lot (hundreds) of repositories I want to keep updated on daily basis, here is a handy script I use:


# saving current working directory
cwd=$(pwd)

echo "reading each repo directory in $cwd"
for repo in *
do
echo '#################################################'
# change to give repo directory
cd $repo
# print repo url
    git config --get remote.origin.url
    git fetch
    git status
    # go back to directory you started with
    cd $cwd
done
}