Autoplay video after ajax call

Here I’m going to show how to play a video automatically after an ajax call.

// this is a snapcode of the js that let the audio play

document.getElementById(“srcVideo”).src=xmlhttp.responseText; document.getElementById(“srcVideo”).play();

And here the HTML code used for the player (video source)

<div id=”player-overlay”>

<video id=”video”>
<sourcesrc=”video_file.mp4″ type=”video/mp4″ id=”srcVideo” />
</video>
</div>

 

Otto an event bus in Android

If you have an app where parts of it need to communicate by exchanging messages (events), you can easily use Otto. What is Otto?

Otto is an event bus used to allowing communication among different parts of the application.

See http://square.github.io/otto/ for more details.

Here I’m going to show a dummy project to see how Otto works. What we want to do is:

  • Fragment 1 sends an event by pressing a button
  • The Activity reads the event and modify a textView of Fragment 2

First of all, we are going to create the Bus Provider class:

package com.savinoordine.otto.test;
import com.squareup.otto.Bus;

public final class BusProvider {
  private static final Bus BUS = new Bus();

public static Bus getInstance() {
  return BUS;
}

private BusProvider() {}
}

 

The application consisting in 2 fragments and an activity. Let’s see first the fragments code:

package com.savinoordine.otto.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Frag1Fragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {

  View view = inflater.inflate(R.layout.fragment_frag1, container, false);
  Button getButton = (Button) view.findViewById(R.id.button_get);
  getButton.setOnClickListener(new View.OnClickListener() {
  @Override
    public void onClick(View v) {
      BusProvider.getInstance().post(new fragm1Event("fragment1OttoEvent") );
  }
});

return view;
}

public class fragm1Event {
 String message;
  public fragm1Event(String message) {
    this.message = message;
  }
 }
}

 

And this is the second fragment:

package com.savinoordine.otto.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag2Fragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
  return inflater.inflate(R.layout.fragment_frag2, container, false);
}
}

 

Let’s see how activity read the event and modify the Fragment 2.

package com.savinoordine.otto.test;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.TextView;
import com.squareup.otto.Subscribe;

public class MainActivity extends Activity {
 FragmentManager fm;

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

@Override
public void onPause() {
  super.onPause();
  BusProvider.getInstance().unregister(this);
}

// you need to register the activity
@Override
public void onResume() {
  super.onResume();
  BusProvider.getInstance().register(this);
}

// Here you get the event and modify the fragment2 (fragment1Event)
@Subscribe
public void onFragm1Event(Frag1Fragment.fragm1Event event) {

  Fragment f2 = fm.findFragmentByTag("f2");
  TextView tv = (TextView) f2.getView().findViewById(R.id.f2_text);
  tv.setText(event.message);
  }
}

 

Here the activity layout with the two fragments (I omitted the fragments layout for clarity)

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<fragment
android:tag="f1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.savino.tryit.app.Frag1Fragment">
</fragment>

<fragment
android:tag="f2"
android:id="@+id/f2_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.savino.tryit.app.Frag2Fragment">
</fragment>

</LinearLayout>

 

 

 

Arduino first steps

Check it here…. Arduino first steps

Android Studio + gradle + Windows 7

Android Studio + Windows 7 + gradle = Gradle project sync failed error

I always got the error below in my log file

 Error occurred during initialization of VM
 Could not reserve enough space for object heap
 Error: Could not create the Java Virtual Machine.
 Error: A fatal exception has occurred. Program will exit.

 

I tried many solutions, as clean project, remove .idea file/folder, restart Android Studio, but no one of those worked for me.

After many trials, I got the final solution, and it perfectly works for me! You need to modify the Xmx value, follow the steps:

  1. Control Panel
  2. System
  3. Advanced(tab)
  4. Environment Variables
  5. System Variables
  6. New
 Variable name: _JAVA_OPTIONS
 Variable value: -Xmx512M

 

…and your nightmare disappears!

Uninstall Applications using ADB

If you need to uninstall an App on your phone, you can do it using adb (Android Debug Bridge).

The path to execute adb is <sdk>/platform-tools

adb uninstall <package name>

 

Tip. To get in the sdk folder, try to execute “cd $ANDROID_HOME

Windows 7.. or maybe not

I use Windows 7 on my PC and today I see a really funny thing… typical of Windows.

As I said, I use Windows 7, but seems “they” forget to change a text on Disk Cleanup window :) …Now I’m confuse, maybe I have Windows Vista and I never knew it!

 

How to ignore “KEY is not translated in LANGUAGE” in Eclipse

If you are using Eclipse, go in Window -> Preferences -> Android -> -Lint Error Checking and find Missing Translation. Change the value to Warning and apply it.

 

 

Otherwise, you can create a lint.xml file in your project like this:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="ExtraTranslation" severity="warning" />
    <issue id="MissingTranslation" severity="warning" />
</lint>

It’s very..

It’s very easy to be different,

but very difficult to be better.

-

Jonathan Ive

CxxTest – Quick start guide

CxxTest is a unit testing framework for C++ that is similar in spirit to JUnit, CppUnit, and xUnit. CxxTest is easy to use because it does not require precompiling a CxxTest testing library, it employs no advanced features of C++ (e.g. RTTI) and it supports a very flexible form of test discovery. [ref. CxxTest]

In this guide, I’ll explain how to create and execute a TestSuite using CxxTest. Let’s start.

1. Download last CxxTest release form here, uncompress it and insert into the same folder of your project. Now you have a new folder in your project called cxxtest_<release_number>

2. Let’s create our first TestSuite (TestSuite1.h).

#include <cxxtest/TestSuite.h>
class TestSuite1 : public CxxTest::TestSuite
  {
  public: void testAddition( void )
      {
          TS_ASSERT( 1 + 2 > 0 );
          TS_ASSERT_EQUALS( 1 + 2, 3 );
      }
  };
To check the assertion list, visit http://cxxtest.com/guide.html#testAssertions

3. After create our TestSuite file, we have to create the executable file. So, let’s open the shell and go in your project folder, then type as follow:

> python cxxtest_<release_number>/bin/cxxtestgen --error-printer -o test1.cpp TestSuite1.h
> g++ -I cxxtest_<release_number>/ test1.cpp -o test
> ./test

In our case, the output of ./test file is Running cxxtest tests (1 test)..OK!

 

It might be useful (possible errors)

I got some errors during the process to create the executable file.

1. No tests defined
No tests defined

This was caused by switching .H and .CPP file

  • wrongpython cxxtest_<release_number>/bin/cxxtestgen –error-printer -o TestSuite1.h test1.cpp
  • right:   python cxxtest_<release_number>/bin/cxxtestgen –error-printer -o test1.cpp TestSuite1.h
2. References
test1.cpp:8:34: error: cxxtest/TestListener.h: No such file or directory
test1.cpp:9:33: error: cxxtest/TestTracker.h: No such file or directory
test1.cpp:10:32: error: cxxtest/TestRunner.h: No such file or directory
test1.cpp:11:38: error: cxxtest/RealDescriptions.h: No such file or directory
test1.cpp:12:30: error: cxxtest/TestMain.h: No such file or directory
test1.cpp:13:34: error: cxxtest/ErrorPrinter.h: No such file or directory

This was caused by wrong cxxtest folder when you create the executable file.

  • wrong: g++ -I cxxtest_<wrong_path>/ test1.cpp -o test
  • right:   g++ -I cxxtest_<release_number>/ test1.cpp -o test

 

3. Create executable file
ld: file too small for architecture x86_64
collect2: ld returned 1 exit status

This was caused by omitting -o parameter while create executable file.

  • wrong: g++ -I cxxtest_<release_number>/ test1.cpp test
  • right:   g++ -I cxxtest_<release_number>/ test1.cpp -o test

If you think..

If you think you are too small to make a difference, 

try sleeping with a mosquito.

-

Dalai Lama