[Sony Cross-Device General] Geeks Empire:
Tutorial to Start Programming for Xperia SmallApps

2. Starting...

- Create New Android Project (Alt + Shift + N)

* Minimum Requirement: API 16

* Target: API 19/20

* Compile with: Sony Add-on SDK 3.0 (API-19)

{If you want to add SmallApp to your current Application Follow from Here.}

- from Bottom to Top

* Open Manifest Add SmallApp Permission

<uses-permission android:name="com.sony.smallapp.permission.SMALLAPP" />

* Inside Application Part Add required uses-library

<uses-library android:name="com.sony.smallapp.framework" />

* SmallApp itself is not an activity, so remove the activity if you want to create only a SmallApp & Add this

<service
android:name="YourPackageName.YourSmallAppClass"
android:exported="true" >
<intent-filter>
<!-- This is the SmallApp Service that will appear on SmallAppDrawer
So if you want to have another SmallApp Service Don't add this Intent filter -->
<action android:name="com.sony.smallapp.intent.action.MAIN" />

<category android:name="com.sony.smallapp.intent.category.LAUNCHER" />
</intent-filter>
</service>

+ If you want that your Application support Xperia Themes (Colors, Shape of Switches & etc.)

In Application Add this;

android:theme="@android:style/Theme.DeviceDefault"

Save Manifest & Close it.

* In res there is no different between SmallApp & Normal Apps

But If you want to create custom theme you can use this

getWindow().setWindowTheme(resId);

+ If want to create custom view for Minimized View (Like the Sample Code)

you should remember it is just 48dp*48dp

* Now Coding...

Open the .java file

To declare your class as SmallApp you should extend it as SmallApplication

After you need to remove Bundle elements + Also onCreateOptionMenu

these two created by default But not support in SmallApplication Class.

Now you have this...

public class SmallMain extends SmallApplication{
@Override
public void onCreate() {
super.onCreate();
setContentView(R.layout.main);
//Title of SmallApp Window By default it will show the app name
setTitle(R.string.app_name);
//if you don't set any custom view it will show the app icon by default
setMinimizedView(R.layout.activity_mini);
}
}

+ (ctrl + shift + O) to import required packages

import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallAppWindow.WindowState;
import com.sony.smallapp.SmallApplication;

3. Check This Figure to Understand Small Apps Lifecycle

Unlike Normal Android Activity There is No Bundle predefined so if you want to use Bundle you need to define it.

For Android Activities When you quit the activity it will go to onPause() & then onDestroy

But onStop does not recognize the Minimized State of SmallApps.

There is Predefine Function in SmallApplication class that can do this as follow;

getWindow().setOnWindowStateChangeListener(new SmallAppWindow.OnWindowStateChangeListener() {
@Override
public void onWindowStateChanged(WindowState state) {
Toast.makeText(getApplicationContext(), "State Change Listener:\n" + state, Toast.LENGTH_SHORT).show();
/*
[B]WindowState.MINIMIZED[/B] >> When Minimize the App
[B]WindowState.NORMAL[/B] >> When App back to normal state
[B]WindowState.FITTED[/B] >> When App Maximized
*/
}
});

+ There is another function that can recognize when user focused on SmallAppWindow Or Not.

getWindow().setOnWindowFocusChangeListener(new SmallAppWindow.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
Toast.makeText(getApplicationContext(), "State Focus Listener:\n" + hasFocus, Toast.LENGTH_SHORT).show();

/*[COLOR=Blue][B]# Example for Usage of these two function;[/B][/COLOR]
Check if SmallAppWindow is in Fitted state & User unfocused from SmallApp automatically minimize the App.*/
WindowState state = getWindow().getWindowState();
if(state == WindowState.FITTED && hasFocus == false){
getWindow().setWindowState(SmallAppWindow.WindowState.MINIMIZED);
}
}
});

4. To set your default Attributes for SmallAppWindow

* Create Resources attrs.xml file in res > values & Add dimensions

<resources>
[I]Minimum Dimensions for SmallAppWindow[/I]
<dimen name="min_width">140dp</dimen>
<dimen name="min_height">200dp</dimen>

[I] Dimensions for First launch of SmallApp
After first launch SmallAppFramework launch last dimensions that change by user [/I]
<dimen name="width">220dp</dimen>
<dimen name="height">300dp</dimen>
</resources>

5. Now You Have an SmallApp Standalone or with your Activities

* If you want to Open SmallApps throw you Activities Or to response from another Applications

* For example if you want to get Shared Image (Like Sample Code) & Display it in SmallApp

You need An Activity to Handle Intent-Filters & then open the smallapp class.

Code below shows how to open SmallApp from Android Activity.

//As you can see this ability is not available in old xperia phones
// it s since SDK 3.0 and needs VERSION.API_LEVEL >= 2 so you need to check this
// or set minimum android requirement to API > 16
if (SdkInfo.VERSION.API_LEVEL >= 2) {
Intent intent = new Intent();
intent.setClass(this, SmallMain.class);
try {
SmallApplicationManager.startApplication(this, intent);
}
catch (SmallAppNotFoundException e) {
//
}
} else {
Toast.makeText(this, "Device Not Supported", Toast.LENGTH_SHORT).show();
}

NOTE: Android Studio need an attention on build.gradle for Phone App

android {
compileSdkVersion '[B]Sony:Sony Add-on SDK 3.0:19[/B]' //Sometimes It will set to API-19 So you have to edit it manually
...
}

^^^

^^^

*** Sample Codes ***

OOOOOOO

​

# Image Picker [Xperia] DOWNLOAD SourceCode

*What this Sample can do...
- Share Image to see it on Small App Window on top of other apps.
- Open App & Pick Image Directly through this app.
- Long Press on Image to Share it to Current App (App Under Small App Window).
- When you minimize the App it will show picked image on minimized view.

*What you get from this code...
- Create Small Apps
- Create Custom View
- Intent & Clipboard & etc.

OOOOOOO

​

# CompassHD+ [Xperia] DOWNLOAD SourceCode/Apk

*What you get from this code...
- Working With Sensors
- NavDrawer (Swipe Menu)
- Setting XML
- Custom View

OOOOOOO

# Task & Process DOWNLOAD SourceCode

*What this Sample can do...
- Show List of All Installed Application (Label & Icon)
- Show Current Process List
- Create Current App List

*What you get from this code...
- How to Work with Package-Manager classes
- & Activity-Manager classes
- Make Task Switcher Application
- Communicate Directly to Activities of other Apps

​

OOOOOOO

# CameraW [Xperia] DOWNLOAD SourceCode/APK

*What you get from this code...
- Camera API (Standalone Camera)
- File Saving (Capturing Pictures & Video)
Note: The Video Recording is not available in the App itself But Code Sample is available.

OOOOOOO

# ScreenTest [SmallApp] DOWNLOAD SourceCode

*What you get from this code...
- Working more with GUI of Activity & SmallApplication
(Full Screen: No NotificationBar, Nav Bar & another control)

OOOOOOO

The explanations of codes are available inside each classes.

Don't forget to Hit Thanks
​