레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시

2015년 12월 20일 일요일

Android ANR Debugging


In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services.
and android will display the Application Not Responding (ANR) dialog for a particular application when it detects one of the following conditions:
  • No response to an input event (within 5 seconds.
  • A BroadcastReceiver hasn't finished executing within 10 seconds.


In case ANR happens, through the method dumpStackTraces() of ActivityManagerService all the information of the thread stack will be saved in /data/anr/ directory.

Then, what we need to find or look at to ?
The following is the testing log snippet...basic investigation steps would be to look for a pattern like "waiting to lock........held by tid=xxx"..
Here Thread ID 12 is hold the task.. so we need to look more closely the problematic method.



"main" prio=5 tid=1 MONITOR
| group="main" sCount=1 dsCount=0 obj=0x410a8c40 self=0xa73c98
| sysTid=704 nice=0 sched=0/0 cgrp=default handle=1075004808
| schedstat=( 0 0 0 ) utm=31475 stm=3384 core=1
  at com.test.studio.TestNativeSAP.synchronizedInvoke(TestNativeSAP.java:~246)
  - waiting to lock <0x42472270> (a com.test.studio.TestNativeSAP) held by tid=12 (AsyncTask #2)

"AsyncTask #2" prio=5 tid=12 NATIVE
  at com.test.studio.TestNativeSAP.startPreview(Native Method)
  at com.test.studio.TestNativeSAP.synchronizedInvoke(TestNativeSAP.java:286)
  at com.test.studio.TestNativeSAP.invokeStartPreview(TestNativeSAP.java:169)

This is the only example.. trace does not always contain "waiting to lock"
so sometimes it is really hard to find main reason.



Thread States:
- running: executing application code
- sleeping: called Thread.sleep()
- monitor: waiting to acquire a monitor lock
- wait: in Object.wait()
- native: executing native code
- vmwait: waiting on a VM resource
- zombie: thread is in the process of dying
- init: thread is initializing
- starting: thread is about to start


Android Testing Library


Android testing support library includes the followings:
1) AndroidJunitRunner: JUnit4 compatible test runner for Android
2) Espresso: UI testing framework, suitable for functional UI testing within an app
3) UI Automator: UI testing framework, suitable for cross-app functional UI testing


1) AndroidJunitRunner
- Looks really familiar with Junit, but should avoid mixing JUnit3 and JUnit4
- For use JUnit4, must use @RunWith(AndroidJUnit4.class) annotation


import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.AndroidJUnitRunner;
import android.test.ActivityInstrumentationTestCase2;

@RunWith(AndroidJUnit4.class)
public class CalculatorInstrumentationTest
        extends ActivityInstrumentationTestCase2 {

    @Before
    public void setUp() throws Exception {
        super.setUp();

        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void typeOperandsAndPerformAddOperation() {
        // Call the CalculatorActivity add() method and pass in some operand values, then
        // check that the expected value is returned.
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}



2) Espresso
In order for the Android Plug-in for Gradle to correctly build and run your Espresso tests,
you must specify the following libraries in the build.gradle file of your Android app module:

dependencies {
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}

Steps for writing codes are easy, check the below steps.
- Find the UI component to test in an Activity by calling onView(), onData()
- Simulate a specific user interaction by calling ViewInteraction.perform(), DataInteraction.perform()
- Make sure everything works as expected

// find a view by looking for a text string it displays 
onView(withText("Sign-in"));

// find a view with resource id
onView(withId(R.id.button_signin));

// allOf() combines multiple matchers together
// find a view that has resource id of button_signin and contain the string "Sign-in"
onView(allOf(withId(R.id.button_signin), withText("Sign-in")));

// with not keyword can filter those views do not matchers
// find a view that has resource id of button_signin but do not contain the string "Sign-in"
onView(allOf(withId(R.id.button_signin), not(withText("Sign-out"))));


In an AdapterView widget, the view is dynamically populated with child views at runtime. If the target view you want to test is inside an AdapterView (such as a ListView, GridView, or Spinner), the onView() method might not work because only a subset of the views may be loaded in the current view hierarchy.

Instead, call the onData() method to obtain a DataInteraction object to access the target view element.
onData(allOf(is(instanceOf(Map.class)),
        hasEntry(equalTo(LongListActivity.ROW_TEXT), is(str))));


The ViewActions class provides a list of helper methods for specifying common actions. You can perform actions like:
- ViewActions.click(): Clicks on the view.
- ViewActions.typeText(): Clicks on a view and enters a specified string.
- ViewActions.scrollTo(): Scrolls to the view. The target view must be subclassed from ScrollView
- ViewActions.pressKey(): Performs a key press using a specified keycode.
- ViewActions.clearText(): Clears the text in the target view.

Lastly call the ViewInteraction.check() or DataInteraction.check() method to assert that the view in the UI matches some expected state.

// Check that the text was changed.
    onView(withId(R.id.textToBeChanged))
            .check(matches(withText(STRING_TO_BE_TYPED)));


3) UI Automator
In order for the Android Plug-in for Gradle to correctly build and run your UI Automator tests, you must specify the following libraries in the build.gradle file of your Android app module:
dependencies {
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}


// Initialize UiDevice instance
private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

// find Cancel/OK button object
UiObject cancelButton = mDevice.findObject(new UiSelector()
        .text("Cancel"))
        .className("android.widget.Button"));
UiObject okButton = mDevice.findObject(new UiSelector()
        .text("OK"))
        .className("android.widget.Button"));

// Simulate a user-click on the OK button, if found.
if(okButton.exists() && okButton.isEnabled()) {
    okButton.click();
}


// With UiSelector, it is possible to access a specific UI component in an app
// the first list view in the currently displayed UI
// then find UI element with the text property "Apps"
UiObject appItem = new UiObject(new UiSelector()
        .className("android.widget.ListView")
        .instance(1)
        .childSelector(new UiSelector()
        .text("Apps")));

Once your test has obtained a UiObject object, you can call the methods in the UiObject class to perform user interactions on the UI component
represented by that object. You can specify such actions as:

- click(): Clicks the center of the visible bounds of the UI element.
- dragTo(): Drags this object to arbitrary coordinates.
- setText(): Sets the text in an editable field, after clearing the field's content.
- swipeUp(): Performs the swipe up action on the UiObject.
Similarly, the swipeDown(), swipeLeft(), and swipeRight() methods perform corresponding actions.


Lastly use standard JUnit Assert methods to test that UI components in the app return the expected results.

private static final String CALC_PACKAGE = "com.myexample.calc";

public void testTwoPlusThreeEqualsFive() {
    // Enter an equation: 2 + 3 = ?
    mDevice.findObject(new UiSelector()
            .packageName(CALC_PACKAGE).resourceId("two")).click();
    mDevice.findObject(new UiSelector()
            .packageName(CALC_PACKAGE).resourceId("plus")).click();
    mDevice.findObject(new UiSelector()
            .packageName(CALC_PACKAGE).resourceId("three")).click();
    mDevice.findObject(new UiSelector()
            .packageName(CALC_PACKAGE).resourceId("equals")).click();

    // Verify the result = 5
    UiObject result = mDevice.findObject(By.res(CALC_PACKAGE, "result"));
    assertEquals("5", result.getText());
}


Reference:
Android Testing Support Library

2015년 12월 16일 수요일

SVG in Android


I believe vector graphics native supports for android had long been on the wish list and good news(?) is that from the release of API 21 (Android 5.0) VectorDrawable has been included. if used properly I don't think we have to prepare all the images for each different device resolutions, however still there exist some problems.
Android supports only basic level of vector graphics..

  • VectorDrawable supports paths using the ‘d’ attribute like the SVG format
  • VectorDrawable does not read/support the SVG format
  • VectorDrawable does not support gradients
  • VectorDrawable does not support numbers using scientific E notation (as commonly used in SVG)


Even though SVG format is not supported, we can still convert SVG files to VectorDrawables with Android Studio and open source tools like Inkscape, svg2android

Menu in Android Studio:
File > New > Vector Asset > Local Asset file > change options if required

Link:
Inkscape: https://inkscape.org/
svg2android: http://inloop.github.io/svg2android/


Maybe SVG not only helps to save some time for development, but also increase the visual quality, we need to be careful of using it, because it is rendered at runtime, so always need to keep the complexity of SVG files to minimum.

2015년 12월 7일 월요일

How to use LRU cache in Android


Android provides LRU cache, that is to evict the least recently used item from the data.
https://developer.android.com/reference/android/util/LruCache.html

1. The size of the cache can be adjusted like the following (depends on the purpose of having cache and the resolution of the devices):
// Cache Size 
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int availableMemoryInBytes = am.getMemoryClass() * 1024 * 1024;

// Need to adjust size
LruCache bitmapCache = new LruCache(availableMemoryInBytes / 8); 


2. In the example we need to override sizeOf() methods with bitmap size
public class MyCache extends LruCache(String, Bitmap) {
    @Override
    protected int sizeOf(String Key, Bitmap value) {
        // return the size of the in-memory bitmap
        // counted against maxSizeBytes
        return value.getByteCount();
    }
    ...
}      


3. The usage of the LRU cache:
Bitmap bitmap = mCache.get(filename);
if (bitmap == null) {
    bitmap = BitmapFactory.decodeFile(filename);
    mCache.put(filename, bitmap );
}

2015년 2월 2일 월요일

How to set your app to open a file in Google Drive

To make sure your app is included in the list of apps that can open a file in Google Drive app, you need to make some additions to your AndroidManiest.xml. The activity for opening Drive files & MIME Types that activity can open must be specified.
The following example code shows the manifest for "ViewerActivity". Your own app manifest must include a similar intent with your own appropriate values and MIME types that your app can open:

<activity
android:name="ViewerActivity"
android:label="@string/my_viewer"
android:icon="@drawable/app_icon"
android:exported="true">

<intent-filter>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN">
<data android:mimetype="image/png">
<data android:mimetype="image/jpeg">
<data android:mimetype="image/jpg">
</intent-filter>
</activity>




Reference:
https://developers.google.com/drive/android/java-client




2014년 9월 12일 금요일

Signing in with Google+ for Android

First step is to go to the Google Developers Console (https://console.developers.google.com/project) and create project as well as Client ID.



Second step would be adding some permissions in AndroidManifest.xml to access Google+ API, account name as part of sign in, OAuth 2.0 tokens or invalidate tokens to disconnect. The following permissions should be requested.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

Now we're ready to add some codes to initialize GoogleApiClient.
Some might wonder what GoogleApiClient really does, it basically wraps a ServiceConnection to Google Play services, this GoogleApiClient object is used to communicate with Google+ API and becomes functional after the asynchronous connection has been established with the service, and typically GoogleApiClient is managed like this:
- initialize GoogleApiClient in Activity.onCreate()
- invoke GoogleApiClient.connect() during Activity.onStart()
- invoke GoolgeApiClient.disconnect() during Acitivity.onStop()


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;

public class PlusActivity extends Activity 
        implements ConnectionCallbacks, OnConnectionFailedListener {

 /* Request code used to invoke sign in user interactions. */
 private static final int RC_SIGN_IN = 0;

 private GoogleApiClient mGoogleClient;

 private ProgressDialog mConnectionProgressDialog;

 /*
  * A flag indicating that a PendingIntent is in progress and prevents us
  * from starting further intents.
  */
 private boolean mIntentInProgress;

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

  mGoogleClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
    .addScope(Plus.SCOPE_PLUS_LOGIN).addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this).build();

  mConnectionProgressDialog = new ProgressDialog(this);
  mConnectionProgressDialog.setMessage("Signing in...");
 }

 @Override
 protected void onStart() {
  super.onStart();
  mGoogleClient.connect();
 }

 @Override
 protected void onStop() {
  super.onStop();

  if (mGoogleClient.isConnected()) {
   mGoogleClient.disconnect();
  }
 }

 @Override
 protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
  if (requestCode == RC_SIGN_IN) {
   mIntentInProgress = false;

   if (!mGoogleClient.isConnecting()) {
    mGoogleClient.connect();
   }
  }
 }

 @Override
 public void onConnectionFailed(ConnectionResult connectionResult) {
  Toast.makeText(this, "GoogleAPIClient Failed to Connect", Toast.LENGTH_SHORT).show();

  if (!mIntentInProgress && connectionResult.hasResolution()) {
   try {
    mIntentInProgress = true;
    startIntentSenderForResult(connectionResult.getResolution().getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
   } catch (SendIntentException e) {
    mIntentInProgress = false;
    mGoogleClient.connect();
   }
  }
 }

 @Override
 public void onConnected(Bundle bundle) {
  Toast.makeText(this, "GoogleAPIClient Connected", Toast.LENGTH_SHORT).show();
  mConnectionProgressDialog.dismiss();
 }

 @Override
 public void onConnectionSuspended(int arg0) {
  Toast.makeText(this, "GoogleAPIClient Connection Suspended", Toast.LENGTH_SHORT).show();
  mGoogleClient.connect();
 }
}

This is it, we done signing in Google+, will try to add some codes for accessing Google+ API to post some comments, retrieving friends list next time.





2014년 9월 10일 수요일

Add Maps to application (Google Maps)

The key class when working with a map object is the GoogleMap class, models the map object within application. Within UI, a map will be represented by either a MapFragment or MapView object.

Google Map handles the following operations automatically

  • connecting to the Google Maps service
  • downloading map tiles
  • displaying tiles on the device screen
  • displaying various controls such as pan & zoom
  • responding to pan & zoom gestures by moving the map and zooming in/out


Google Maps provides four types of maps: Normal, Hybrid, Satellite, Terrain.
To set the type of a map, use setMapType() of GoogleMap object.
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

Basic steps for adding a map to an Android application
1. Add a fragment
Add a <fragment> element to the Activity's layout file to define a Fragment object, and set android:name attribute to "com.google.android.gms.maps.MapFragment. This will attach a MapFragment to the Activity automatically.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:name="com.google.android.gms.maps.MapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
We can also add a MapFragment to an Activity in code like the following:
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();

2. Add map code
After setting the layout file as the content view for the Activity, get a handle to the map by calling findFragmentById() to set some initial options for the map.

mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

3. Verify map availability
Before interacting with a GoogleMap object, we need to make sure that an object can be instantiated.
Check the following example code snippets, this methods can be called from both onCreate() and onResume() to ensure that the map is always available.

private void setupMapIfNeeded() {
  // null check to confirm that the map has not been instantiated
  if (mGoolgeMap == null) {
    mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    
    if (mGoogleMap != null) {
       // verified, it's ok to manipulate the map
    }
  }
}


2014년 9월 9일 화요일

Supporting different layouts & bitmaps

Create different layouts for each screen size you want to support to optimize user experience. Each layout file should be saved into the appropriate resource directory, named with a -<screen_size> suffix.

e.g.
My Project
       - res/
             - layout/                     ==> default portrait
                         main.xml
             - layout-land              ==> landscape
                         main.xml
             - layout-large/            ==> large portrait
                         main.xml
             - layout-large-land     ==> large landscape
                         main.xml

The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.


Providing bitmap resources that are properly scaled to each of generalized densities: low, medium, high, etc. will help to achieve good graphical quality and performance on all screen densities.

To generate images, start with raw resources in vector format and generate the images for each density using the following size scales:

  • xxxhdpi: 4.0
  • xxhdpi: 3.0
  • xhdpi: 2.0
  • hdpi: 1.5
  • mdpi: 1.0 (baseline)
  • ldpi: 0.75
If we generate a 200x200 image for xhdpi, we should generate the same resources in 150x150 for hdpi, 100x100 for mdpi, 300x300 for xxhdpi and then place them in the appropriate drawable resource directory:

e.g.

My Project
       - res/
             - drawable-xxhdpi/                  
                         example.png
             - drawable-xhdpi            
                         example.png
             - drawable-hdpi/
                         example.png
             - drawable-mdpi
                         example.png



Controlling App's Availability to Devices

Android supports a variety of features, some of them are hardware-based, some are software-based, and some are dependent on platform version. So app developer may need to control app's availability to devices based on app's required features.

We can restrict app's availability to devices through Google Play Store based on the following device characteristics:

  1. Device features
  2. Platform version
  3. Screen configuration

Device features:
Android defines feature IDs for any hardware or software feature that may not be available on all devices. So we can prevent users from installing app when their devices don't provide a given feature by declaring it with a <uses-feature> element in app's manifest file.

e.g. declaring the compass sensor as required
<uses-feature android:name="android.hardware.sensor.compass"
                       android:required="true" />

Google Play Store compares the features app requires to the features available on user's device to determine whether the app is compatible with each device. If the device does not provide all the features, the user cannot install the app.

In case device features are not required for running app's primary functionality, then set the required attribute to "false"  and check for device feature at runtime.
PackageManager pm = getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {
  // No compass available on this device, turn off the compass feature
  disableCompassFeature();
}


Platform version:
Different devices may run different versions of Android, which means new APIs of newer Android version may not be available on older versions of Android.

API level allows to declare the minimum version with which your app is compatible using <uses-sdk> element in app's manifest file.

e.g.
<uses-sdk android:minSdkVersion="14"
                 android:targetSdkVersion="19" />

minSdkVersion: declares the minimum version with which app is compatible
targetSdkVersion: declares the highest version on which app has been optimized

We can check the API level at runtime and gracefully degrade the corresponding features when the API level is too low.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
  // running on something older than API 11, so disable features
  disableSomeFeature();
}

Screen configuration:
Android runs on devices of various sizes from smart phones to TVs. In order to categorize devices by screen type, Android defines two characteristics for each devices: screen size (physical size) and screen density (DPI)

  • generalized sizes: small, normal, large and xlarge
  • generalized densities: mdpi (medium), hdpi, xhdpi (extra high), xxhdpi (extra-extra high)
By default most apps are compatible with all screen sizes and densities because the system makes all the adjustments to UI layout and image resources as necessary for each screen. However we can optimize the user experience for each screen configuration by adding specialized layouts for different screen sizes and optimized bitmap images for common screen densities.

For more information in detail, check the following developers site:



How to use ActionProvider Or ShareActionProvider

ActionProvider can generate action views for use in the action bar, dynamically populate submenus of a MenuItem, and handle default menu item invocations.

Two ways to use an action provider:
- Set the action provider on a MenuItem directly by calling setActionProvider (ActionProvider)
- Declare the action provider in an XML menu resource.   e.g.
  <item
    android:id="@+id/action_search"
    android:title="Search"
    android:icon="@drawable/search_icon"
    android:showasaction="ifRoom"
    android:actionproviderclass="com.my.CustomActionProvider" />

Making a custom action provider is straightforward, simply extend ActionProvider class and implement its callback methods as appropriate. It should be implemented like below code example:

public class MyCustomActionProvider extends ActionProvider {
   private Context mContext;
   private EditText mEditText;

   public MyCustomActionProvider (Context context) {
     super(context);
     mContext = context;
   }

   @override
   public View onCreateActionView() {
     // Make & return View that will be shown when action bar icon pressed
     // Here we have a custom layout that contains EditText for Search

     LayoutInflator layout = LayoutInflator.from(mContext);
     View view = layout.inflate(R.layout.search_layout, null);
     mEditText = (EditText) view.findViewById(R.id.edit_search);
     ...................
   }

   ................
}

public class MainActivity extends Activity {
  private MyCustomActionProvider mActionProvider;
  ...........

  @override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.items, menu);
    MenuItem item = menu.findItem(R.id.action_search);
    mActionProvider = (MyCustomActionProvider) item.getActionProvider();
    ..............
  }
  
  ...........
}

Android also provides an implementation of ActionProvider for Share actions: ShareActionProvider, which facilitates a share action by showing a list of possible apps for sharing directly in the action bar.
To add a share action with ShareActionProvider, just define the tag  for an <item> android:actionproviderclass="android.widget.ShareActionProvider".

The last step left to do is to define the Intent what we want to use for sharing. How we do it? just modify onCreateOptionMenu() method.
// In Activity#onCreateOptionsMenu
 public boolean onCreateOptionsMenu(Menu menu) {
     // Get the menu item.
     MenuItem menuItem = menu.findItem(R.id.my_menu_item);
     // Get the provider and hold onto it to set/change the share intent.
     mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
     // Set history different from the default before getting the action
     // view since a call to MenuItem.getActionView() calls
     // onCreateActionView() which uses the backing file name. Omit this
     // line if using the default share history file is desired.
     mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
     . . .
 }

 // Somewhere in the application.
 public void doShare(Intent shareIntent) {
     // When you want to share set the share intent.
     mShareActionProvider.setShareIntent(shareIntent);
 }

2014년 9월 2일 화요일

How to set/get Meta Data in AndroidManifest

In AndroidManifest.xml


Meta data defined in AndroidManifest.xml can be retrieved with some codes like this:

try {
    ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("com.myapp.version.api.key");
} catch (NameNotFoundException e) {
    Log.e(TAG, "Failed to load meta data : " + e.getMessage());
} catch (NullPointerException e) {
    Log.e(TAG, "Failed to load meta data : " + e.getMessage());         
}

2014년 8월 27일 수요일

How to check the package name of current launcher


Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String packageName = resolveInfo.activityInfo.packageName;

2014년 8월 3일 일요일

How to take a snapshot in Google Map v2

// Once everything has loaded, cache the map’s image for fast display later
// Note: the map loaded callback is only called once.
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    public void onMapLoaded() {
        mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
            public void onSnapshotReady(Bitmap bitmap) {
                // Write image to disk
            }
        });
    }
});
Reference: http://googlegeodevelopers.blogspot.sg/2013/10/ghost-markers-in-your-neighborhood-new.html

How to change text color of SearchView

int textViewId = mSearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) mSearchView.findViewById(textViewId);
textView.setTextColor(Color.GRAY);

2014년 7월 13일 일요일

Useful Android Intents

Useful Android Intent List

1. Google Maps - get directions between two points
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=0,0&daddr=10,10"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
startActivity(intent);

2. Google Maps - show map
Uri uri = Uri.parse("geo:11.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW, uri);
startActivity(it);
3. Call
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);  
startActivity(it); 
OR
with permission 

Uri uri = Uri.parse("tel.xxxxxx");
Intent it = new Intent(Intent.ACTION_CALL,uri);
4. SMS/MMS
Uri uri = Uri.parse("smsto:0800000123");   
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
it.putExtra("sms_body", "The SMS text");   
startActivity(it); 

Uri uri = Uri.parse("content://media/external/images/media/23");   
Intent it = new Intent(Intent.ACTION_SEND);   
it.putExtra("sms_body", "some text");   
it.putExtra(Intent.EXTRA_STREAM, uri);   
it.setType("image/png");   
startActivity(it);
5. Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);


Intent it = new Intent(Intent.ACTION_SEND);   
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
it.setType("text/plain");   
startActivity(Intent.createChooser(it, "Choose Email Client")); 


Intent it = new Intent(Intent.ACTION_SEND);     
String[] tos = {"me@abc.com"};     
String[] ccs = {"you@abc.com"};     
it.putExtra(Intent.EXTRA_EMAIL, tos);     
it.putExtra(Intent.EXTRA_CC, ccs);     
it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
it.setType("message/rfc822");     
startActivity(Intent.createChooser(it, "Choose Email Client"));  


// Adding extra
Intent it = new Intent(Intent.ACTION_SEND);   
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
sendIntent.setType("audio/mp3");   
startActivity(Intent.createChooser(it, "Choose Email Client"));
6. Play Music
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);


Uri uri = Uri.withAppendedPath(
MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
Intent it = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(it); 
7. Uninstall apk
Uri uri = Uri.fromParts("package", PackageName, null);   
Intent it = new Intent(Intent.ACTION_DELETE, uri);   
startActivity(it);
8. Search application in Google Play Store
Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
Intent it = new Intent(Intent.ACTION_VIEW, uri);  
startActivity(it);  
9. Google Search
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);

2014년 6월 28일 토요일

[Binder] Native Service Example 2

I'll try to finish off Client part



class BpLcdService : public BpInterface
{
public:
 BpLcdService(const sp& impl): BpInterface(impl) {}

 virtual void LcdOn()
 {
  Parcel data, reply;
  remote()->transact(LCD_ON, data, &reply);
 }

 virtual void LcdOff()
 {
  Parcel data, reply;
  remote()->transact(LCD_OFF, data, &reply);
 }
};
IMPLEMENT_META_INTERFACE(LcdService, "android.app.lcdcontrol")

sp<ILcdService> LcdService()
{
sp<IBinder> binder = defaultServiceManager()->getService(String16("LcdService"));
return interface_cast<ILcdService>(binder);
}


This client does not have main() as I intended to make this as shared library file (.so) so I can call LcdOn/Off methods from Java application with Java Native Interface.

[Binder] Native Service Example 1

Here I'm going to make a native service for turning on/off LCD screen, and a client that make a request to the native service. At the end I will try to install a client application to my Android phone and see whether it actually work as intended.

Basically the flows looks like this:



Android uses some prefixes like Bp, Bn,
Bp stands for Binder Proxy (client side) whereas Bn stands for Binder Native, this one is for service server side. That's why we have BpLcdService, BnLcdService.

ok .. firstly in order to communicate between client and server, there should be some sort of protocol
so we define ILcdService that contains only two virtual methods. Whoever that use this protocol must provide LcdOn/Off methods.

class ILcdService : public IInterface
{
public:
       DECLARE_META_INTERFACE(LcdService)
       enum { LCD_ON = 1, LCD_OFF };

       virtual void LcdOn() = 0;
       virtual void LcdOff() = 0;
};

IInterface (in Android F/W) provides asBinder() method to convert the type of ILcdServce to IBinder. Why we need this conversion is that during IPC , IBinder type object will be stored in RPC data and sent to Binder driver. In other words say when registering LcdService to the System, Service Manager needs to send RPC data with service object (BBinder type) to Context Manager. In this process service will be converted to IBinder type and sent to Binder Driver.




2014년 6월 27일 금요일

What's Binder?

Everyone including app developers in Android world  may have heard about Binder, but I was wondering what exactly binder is and how that works. the following is what I've found so far.

Brief History of Binder?
Binder was originally made by George Hoffman as OpenBinder project for BeOS (I do not know why that lived for short time, will appreciate if anyone go find out and share some ideas/opinions)
Anyway OpenBinder project was carried out by Dinnie Hackborn after Be Inc. was acquired by Palm... and Dinnie moved to Google >>>> developed Binder in Android what we're using now.

Why use Binder? We can still use Inter Process Communication like sockets and pipes. Answer for this question would be performance. In Android all system functions are provided as server processes that means an optimized and fast communication method is required. Binder use a kernel memory, that's shared between all processes to minimize memory copy overhead, and provides Remote Procedure Call as well.

Benefits of adopting Binder mechanism seems very similar to those of Microkernel OS.
- It's easy to add a new service or remove an existing function
- It's not necessary to test the entire services, only limited components => easy to test
- Communication between processes is handled through Binder, so it guarantees transparency between components.


The following diagram depicts how Binder works in Android system, as previously mentioned two processes communicates through Binder





2014년 6월 26일 목요일

How to Copy & Paste in Android (Clipboard)

When we run applications like Google Drive, Gmail, or any text editing apps, it's easy to copy and paste something. it's possible to grab an image from browser and paste it in memo app as well.

how does this work?
If you go through Android Developers site, you can find ClipBoard Framework.
Basically Android provides ClipBoard Framework for specifially Copy & Paste.
The following digram (taken from Android Developers site) shows flow and how this works.




Google provides basic fundamental framework and I believe manufacturers like LG, Samsung did some customization this framework, for example on my LG phone, custom UI appears when copying some images & texts on browser (allowing max. 10 items to be stored in clipboard)
 
Anyway it's really easy to use ClipBoard Framework
The text that's being copied to Clipboard here is CopyText, the selected & copied text from TextView or EditText. The code snippets is really simple and clear.

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

ClipData clip = ClipData.newPlainText("Clip",CopyText);
Toast.makeText(mContext, "Copied to Clipboard", Toast.LENGTH_SHORT).show();
clipboard.setPrimaryClip(clip);


In order to paste, firstly check if  there exist a clip then get Item from clip. That's all.
I'm ready to paste.. done.

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

if(clipboard.hasPrimaryClip()== true){
 ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
 pasteText = item.getText().toString();
 pasteTxt.setText(pasteText);
}