Commit e0d1b134 authored by Hermann Mayer's avatar Hermann Mayer

Implemented connection activity and first section fragment of the remote control.

parent ad924227
......@@ -3,7 +3,10 @@
package="net.hermannmayer.remotekit"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
......@@ -12,5 +15,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="RemoteActivity" />
</application>
</manifest>
......@@ -89,4 +89,21 @@
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
<target name="install" depends="debug">
<exec executable="adb">
<arg value="install" />
<arg value="-r" />
<arg value="bin/remotekit-debug.apk" />
</exec>
<exec executable="adb">
<arg value="shell" />
<arg value="am" />
<arg value="start" />
<arg value="-n" />
<arg value="net.hermannmayer.remotekit/net.hermannmayer.remotekit.MainActivity" />
</exec>
</target>
</project>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="15dip" >
<TextView
android:id="@+id/system_volume_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_system_volume_headline"
android:textStyle="bold"
android:typeface="sans"
android:textSize="18sp" />
<TextView
android:id="@+id/system_volume_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/system_volume_title"
android:layout_marginTop="5dip"
android:layout_marginRight="50dip"
android:text="@string/tab_system_volume_description"
android:textSize="14sp" />
<ImageButton
android:id="@+id/system_volume_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/system_volume_title"
android:layout_alignParentRight="true"
android:src="@drawable/ic_audio_vol" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#909194"
android:layout_marginTop="8dp"
android:layout_below="@id/system_volume_description" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="15dip" >
<TextView
android:id="@+id/system_pm_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_system_pm_headline"
android:textStyle="bold"
android:typeface="sans"
android:textSize="18sp" />
<TextView
android:id="@+id/system_pm_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/system_pm_title"
android:layout_marginTop="5dip"
android:layout_marginRight="50dip"
android:text="@string/tab_system_pm_description"
android:textSize="14sp" />
<ImageButton
android:id="@+id/system_pm_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/system_pm_title"
android:layout_alignParentRight="true"
android:src="@drawable/ic_lock_power_off" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#909194"
android:layout_marginTop="8dp"
android:layout_below="@id/system_pm_description" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/volume_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp" >
<SeekBar
android:id="@+id/volume_seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/pm_suspend"
android:title="@string/pm_mode_suspend" />
<item
android:id="@+id/pm_hibernate"
android:title="@string/pm_mode_hibernate" />
<item
android:id="@+id/pm_halt"
android:title="@string/pm_mode_halt" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pm_modes_array">
<item>Suspend</item>
<item>Hibernate</item>
<item>Halt</item>
<string-array name="section_titles_array">
<item>System</item>
<item>Desktop</item>
</string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Remotekit</string>
<string name="tab_connect_hostname">Hostname</string>
<string name="tab_connect_port">Port</string>
<string name="tab_connect_connect">Verbinden</string>
<string name="tab_connect_success">Verbindung hergstellt</string>
<string name="tab_connect_fail">Verbindung fehlgeschlagen</string>
<string name="tab_system_volume_headline">Aktuelles Volume (%1d%%)</string>
<string name="tab_system_volume_description">Ändern der Systemlautstärke.</string>
<string name="tab_system_pm_headline">Powermanagement</string>
<string name="tab_system_pm_description">Das System in einen Schlafzustand versetzen.</string>
<string name="dummy_text">Dummy Sektion %1d.</string>
<string name="connect_hostname_is_empty">Hostname ist leer.</string>
<string name="connect_port_is_empty">Port ist leer.</string>
<string name="dialog_volume_title">Systemlautstärke ändern</string>
<string name="ok">Ok</string>
<string name="pm_mode_suspend">Standby</string>
<string name="pm_mode_hibernate">Ruhezustand</string>
<string name="pm_mode_halt">Herunterfahren</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="section_titles_array">
<item>Connect</item>
<item>System</item>
<item>Desktop</item>
</string-array>
......
......@@ -15,4 +15,14 @@
<string name="tab_system_pm_description">Put the system in an sleep mode.</string>
<string name="dummy_text">Dummy Section %1d.</string>
<string name="connect_hostname_is_empty">Hostname is empty.</string>
<string name="connect_port_is_empty">Port is empty.</string>
<string name="dialog_volume_title">Change master volume</string>
<string name="ok">Ok</string>
<string name="pm_mode_suspend">Suspend</string>
<string name="pm_mode_hibernate">Hibernate</string>
<string name="pm_mode_halt">Halt</string>
</resources>
package net.hermannmayer.remotekit;
import android.app.Activity;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
......@@ -15,21 +16,26 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import android.content.Context;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
import android.content.Intent;
import android.util.Log;
import net.hermannmayer.remotekit.rest.RESTClientTask;
/**
* MainActivity
*
* Startpoint of the application.
*
* @uses Activity
* @author Hermann Mayer <hermann.mayer92@gmail.com>
*/
public class MainActivity extends Activity
{
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adapter that will return a fragment for
// each of the three primary sections of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
setContentView(R.layout.connect);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
......@@ -38,137 +44,81 @@ public class MainActivity extends FragmentActivity implements ActionBar.TabListe
// enabled, since there is no hierarchical parent.
actionBar.setHomeButtonEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and
// setting up a listener for when the user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
final EditText hostnameEl = (EditText) findViewById(R.id.tab_connect_hostname);
final EditText portEl = (EditText) findViewById(R.id.tab_connect_port);
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
hostnameEl.setText("10.0.0.11");
portEl.setText("1337");
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
public class AppSectionsPagerAdapter extends FragmentPagerAdapter
{
private String[] sectionTitles;
final Activity rootActivity = this;
public AppSectionsPagerAdapter(FragmentManager fm)
findViewById(R.id.tab_connect_connect).setOnClickListener(new View.OnClickListener()
{
super(fm);
// Load all section titles
Resources res = getResources();
sectionTitles = res.getStringArray(R.array.section_titles_array);
}
@Override
public Fragment getItem(int i)
{
switch (i) {
case 0:
// The first section of the app is the connect formula
// where a hostname and a port will be given by the user.
return new ConnectSectionFragment();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public void onClick(View view)
{
final String hostname = hostnameEl.getText().toString();
final String port = portEl.getText().toString();
if (0 == hostname.length()) {
// Show a toast notification
Toast toast = Toast.makeText(
getApplicationContext(),
getString(R.string.connect_hostname_is_empty),
Toast.LENGTH_SHORT
);
toast.show();
return;
}
if (0 == port.length()) {
// Show a toast notification
Toast toast = Toast.makeText(
getApplicationContext(),
getString(R.string.connect_port_is_empty),
Toast.LENGTH_SHORT
);
toast.show();
return;
}
RESTClientTask task = new RESTClientTask(hostname, port)
{
@Override
protected void onPostExecute(String[] results)
{
CharSequence msg = "";
@Override
public int getCount()
{
return sectionTitles.length;
}
if (results[0].length() > 0 && -1 != results[0].indexOf("remotekit-server")) {
@Override
public CharSequence getPageTitle(int position)
{
return sectionTitles[position];
}
}
msg = getString(R.string.tab_connect_success);
public class ConnectSectionFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.connect, container, false);
Intent intent = new Intent(rootActivity, RemoteActivity.class);
intent.putExtra("EXTRA_HOSTNAME", hostname);
intent.putExtra("EXTRA_PORT", port);
startActivity(intent);
rootView.findViewById(R.id.tab_connect_connect)
.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Context context = getApplicationContext();
CharSequence text = getString(R.string.tab_connect_success);
int duration = Toast.LENGTH_SHORT;
} else {
msg = getString(R.string.tab_connect_fail);
}
Toast toast = Toast.makeText(context, text, duration);
// Show a toast notification
Toast toast = Toast.makeText(
getApplicationContext(),
msg,
Toast.LENGTH_SHORT
);
toast.show();
}
});
return rootView;
}
}
};
public static class DummySectionFragment extends Fragment
{
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dummy, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.dummy_text, args.getInt(ARG_SECTION_NUMBER)));
return rootView;
}
task.execute(new String("/"));
}
});
}
}
This diff is collapsed.
package net.hermannmayer.remotekit.rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
import android.os.AsyncTask;
import java.util.ArrayList;
public class RESTClientTask extends AsyncTask<String, Void, String[]>
{
private String hostname;
private String port;
private String method;
public RESTClientTask(String hostname, String port)
{
super();
this.hostname = hostname;
this.port = port;
this.method = "GET";
}
public RESTClientTask(String hostname, String port, String method)
{
super();
this.hostname = hostname;
this.port = port;
this.method = method;
}
@Override
protected String[] doInBackground(String... paths)
{
ArrayList<String> responses = new ArrayList<String>();
for (String path : paths) {
responses.add(doRequest(path));
}
String[] resArr = new String[responses.size()];
resArr = responses.toArray(resArr);
return resArr;
}
private String doRequest(String path)
{
try {
URL url = new URL("http://" + hostname + ":" + port + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuffer output = new StringBuffer();
String tmpBuf;
while ((tmpBuf = br.readLine()) != null) {
output.append(tmpBuf + "\n");
}
conn.disconnect();
return output.toString();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return new String();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment