Google vs Apple

Extract from ZDNet.com:

Apple has been asking for it, and now they’re getting it.

Cupertino has been doing its best to sue Samsung’s Android tablets and smartphones out of the market rather than compete with them. Now, Motorola — under Google’s control — is returning the favor. Motorola Mobility is asking the U.S. International Trade Commission (ITC) to ban the import of iPhones, iPads, and Macs.

Welcome to the next logical step in the world of patent warfare: mutually assured destruction (MAD).

For those of you who didn’t grow up during the Cold War between the West and the Soviet Union, here’s how MAD worked.

In MAD-driven foreign policy, the U.S. and the Soviet Union never let things get too ugly between the superpowers because if one of them went too far, the other could bomb the other into the Stone Age and vice-versa. So, yes, there were all kinds of wars from the ’50s through the ’80s — Afghanistan, Angola, Korea, and Vietnam — but the great powers never launched nukes at each other.

MAD was a cold, hard policy; but it worked.

Full article here.

Advertisements

Sleep Tight 1.6.1 source

Hi, although Sleep Tight is my first Android App, I want to share it because it can help someone or maybe somebody can help me to improve it. But this is NOT a tutorial nor a masterclass, it’s only a set of examples that maybe can help to understand some concepts.

Sleep Tight has several files:

  • Sleep.java (main activity class)
  • Reboot.java / Turnoff.java / Turnon.java (broadcast recivers)
  • main.xml (layout)
  • AndroidManifest.xml

AndroidManifest.xml

AndroidManisfest.xml declares one activity to handle the main class, and three broacast recivers to handle application reboot, turn on, and turn off of alarms.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.prog.sleep"
      android:versionName="1.6.1" android:versionCode="12">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Sleep"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<receiver  android:process=":remote1" android:name="Turnoff"></receiver>
		<receiver  android:process=":remote2" android:name="Turnon"></receiver>
		<receiver android:name="Reboot">
			<intent-filter>
				<action android:name="android.intent.action.ACTION_SHUTDOWN" />
			</intent-filter>
		</receiver>
    </application>
</manifest>

main.xml

This is the main layout, a basic TableLayout with buttons and TextViews.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    
    <TableRow>
       	<Button 
       		
       		android:text="Setup Sleep time"
       		android:id="@+id/button_apagar"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="center"
            android:padding="3dip"
            android:id="@+id/sleepview" />
	</TableRow>

	<TableRow>
       	<Button 
       		android:text="Setup awake time"
       		android:id="@+id/button_encender"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="center"
            android:padding="3dip"
            android:id="@+id/awakeview" />
	</TableRow>
	<TableRow>
		<TextView
            android:text=""
            android:gravity="center"
            android:padding="3dip" />
       	<Button 
       		android:text="Remove all alarms"
       		android:id="@+id/removealarms"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
    </TableRow>
    <TableRow>
		<TextView
            android:text="Days:"
            android:gravity="center"
            android:padding="3dip" />
       	
    </TableRow>
    <TableRow>
       	<Button 
       		android:text="Monday"
       		android:id="@+id/button_monday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/mondayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Tuesday"
       		android:id="@+id/button_tuesday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/tuesdayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Wednesday"
       		android:id="@+id/button_wednesday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/wednesdayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Thursday"
       		android:id="@+id/button_thursday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/thursdayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Friday"
       		android:id="@+id/button_friday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/fridayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Saturday"
       		android:id="@+id/button_saturday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/saturdayView" />
	</TableRow>
	<TableRow>
       	<Button 
       		android:text="Sunday"
       		android:id="@+id/button_sunday"
       		android:layout_height="60px"
       		android:gravity="center">
       	</Button>
	    
        <TextView
            android:text="Textview"
            android:gravity="left"
            android:padding="3dip"
            android:id="@+id/sundayView" />
	</TableRow>

</TableLayout>

Turnoff.java and Turnon.java

The Turnoff and turnon classes are triggered by AlarmManager. The main activity of those files are turn on or turn off the sounds of the phone via AudioManager.

package com.prog.sleep;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;

public class Turnoff extends BroadcastReceiver {
	 @Override
	 public void onReceive(Context context, Intent intent) {
		 AudioManager aManager=(AudioManager)context.getSystemService(Sleep.AUDIO_SERVICE);

	     Calendar cal = Calendar.getInstance();
	     int diasetmana=cal.get(Calendar.DAY_OF_WEEK);
	     SharedPreferences settings_read = context.getSharedPreferences("sleep_prefs", 0);
	     // reboot tindra el contingut de si s'ha rebotat o no
	     //System.out.println(work);
	     //System.out.println(diasetmana);
	     String dilluns = settings_read.getString("dilluns", "no");
	     String dimarts = settings_read.getString("dimarts", "no");
	     String dimecres = settings_read.getString("dimecres", "no");
	     String dijous = settings_read.getString("dijous", "no");
	     String divendres = settings_read.getString("divendres", "no");
	     String dissabte = settings_read.getString("dissabte", "no");
	     String diumenge = settings_read.getString("diumenge", "no");
	     //System.out.println(work);
	     //System.out.println(diasetmana);
	     if (diasetmana==Calendar.MONDAY &&  dilluns.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.TUESDAY &&  dimarts.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.WEDNESDAY &&  dimecres.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.THURSDAY &&  dijous.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.FRIDAY &&  divendres.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.SATURDAY &&  dissabte.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     if (diasetmana==Calendar.SUNDAY &&  diumenge.equals("yes")) {
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION ,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,0,0);
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	     }
	     
	 }
}
package com.prog.sleep;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;

public class Turnon extends BroadcastReceiver {
	
	 @Override
	 public void onReceive(Context context, Intent intent) {
		 AudioManager aManager=(AudioManager)context.getSystemService(Sleep.AUDIO_SERVICE);
	     Calendar cal = Calendar.getInstance();
	     int diasetmana=cal.get(Calendar.DAY_OF_WEEK);
	     SharedPreferences settings_read = context.getSharedPreferences("sleep_prefs", 0);
	     // reboot tindra el contingut de si s'ha rebotat o no
	     String dilluns = settings_read.getString("dilluns", "no");
	     String dimarts = settings_read.getString("dimarts", "no");
	     String dimecres = settings_read.getString("dimecres", "no");
	     String dijous = settings_read.getString("dijous", "no");
	     String divendres = settings_read.getString("divendres", "no");
	     String dissabte = settings_read.getString("dissabte", "no");
	     String diumenge = settings_read.getString("diumenge", "no");
	     //System.out.println(work);
	     //System.out.println(diasetmana);
	     if (diasetmana==Calendar.MONDAY &&  dilluns.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.TUESDAY &&  dimarts.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.WEDNESDAY &&  dimecres.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.THURSDAY &&  dijous.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.FRIDAY &&  divendres.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.SATURDAY &&  dissabte.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     if (diasetmana==Calendar.SUNDAY &&  diumenge.equals("yes")) {
	    	 //aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    	 aManager.setStreamVolume(AudioManager.STREAM_SYSTEM,aManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,aManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_MUSIC,aManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_ALARM,aManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),0);
	    	 aManager.setStreamVolume(AudioManager.STREAM_DTMF,aManager.getStreamMaxVolume(AudioManager.STREAM_DTMF),0);
	     }
	     
	  
	     
	 }


}

Reboot.java

The Reboot class is called in case of a ‘reboot event’ and save the state on sharedPreferences.

package com.prog.sleep;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class Reboot extends BroadcastReceiver{
	

@Override
public void onReceive(Context context, Intent intent) {
	SharedPreferences settings = context.getSharedPreferences("sleep_prefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("reboot_string", "si");
    // Commit the edits!
    editor.commit();

}
}

Sleep.java

Sleep.java is the main class. That class create the Intents, controls all shadredPreferences and files, setup AlarmManager and implements the TimePickerDialog and button listeners.

package com.prog.sleep;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Sleep extends Activity {

//Saving awake/sleep time strings
String awake_file = "awake_file";
String sleep_file = "sleep_file";
String dilluns,dimarts,dimecres,dijous,divendres,dissabte,diumenge;
//TextViews from layout
TextView mostra_awake;
TextView mostra_sleep;
TextView dillunsView;
TextView dimartsView;
TextView dimecresView;
TextView dijousView;
TextView divendresView;
TextView dissabteView;
TextView diumengeView;
//Intents for awake/sleep
Intent intent_off;
PendingIntent sender_off;
Intent intent_on;
PendingIntent sender_on;
SharedPreferences settings;
SharedPreferences.Editor editor;
//SharedPreferences settings = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings.edit();

//Pad function to show times correctly
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}

//Read awake/sleep file
private String readFile(String FILENAME) {
try {
FileInputStream fin = openFileInput(FILENAME);
InputStreamReader isReader = new InputStreamReader(fin);
char[] buffer = new char[30];
// Fill the buffer with data from file
isReader.read(buffer);
return new String(buffer);
} catch (Exception e) {
//debug
//Log.i("ReadNWrite, readFile()", "Exception e = " + e);
if (FILENAME=="awake_file") return "No awake time available"; else return "No sleep time available";
}

}

private TimePickerDialog.OnTimeSetListener mTimeSetListener_off = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar cal = Calendar.getInstance();
Calendar horaidataactual = Calendar.getInstance();
if (horaidataactual.get(Calendar.HOUR_OF_DAY)>hourOfDay) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_YEAR,horaidataactual.get(Calendar.DAY_OF_YEAR)+1);
} else {
if (horaidataactual.get(Calendar.HOUR_OF_DAY)==hourOfDay && horaidataactual.get(Calendar.MINUTE)>=minute) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_YEAR,horaidataactual.get(Calendar.DAY_OF_YEAR)+1);
} else {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
}
}
intent_off = new Intent(Sleep.this, Turnoff.class);
sender_off = PendingIntent.getBroadcast(Sleep.this, 0, intent_off, 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender_off);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, sender_off);
Toast.makeText(Sleep.this, "Sound will be disabled at "+pad(hourOfDay)+":"+pad(minute), Toast.LENGTH_SHORT).show();
try {
OutputStream os = openFileOutput(sleep_file, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write("Sleep time at "+pad(hourOfDay)+":"+pad(minute)+"\n");
osw.close();
} catch (Exception e) {
//Debug
//Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
}
TextView mostra_sleep = (TextView) findViewById(R.id.sleepview);
mostra_sleep.setText(readFile(sleep_file));

}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener_on = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar cal = Calendar.getInstance();
Calendar horaidataactual = Calendar.getInstance();
//int hora;
//hora=horaidataactual.get(Calendar.HOUR_OF_DAY);
if (horaidataactual.get(Calendar.HOUR_OF_DAY)>hourOfDay) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_YEAR,horaidataactual.get(Calendar.DAY_OF_YEAR)+1);
} else {
if (horaidataactual.get(Calendar.HOUR_OF_DAY)==hourOfDay && horaidataactual.get(Calendar.MINUTE)>=minute) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_YEAR,horaidataactual.get(Calendar.DAY_OF_YEAR)+1);
} else {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
}
}

intent_on = new Intent(Sleep.this, Turnon.class);
sender_on = PendingIntent.getBroadcast(Sleep.this, 0, intent_on, 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender_off);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, sender_on);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), sender_on);
Toast.makeText(Sleep.this, "Sound will be enabled at "+pad(hourOfDay)+":"+pad(minute), Toast.LENGTH_SHORT).show();
try {
OutputStream os = openFileOutput(awake_file, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write("Awake time at "+pad(hourOfDay)+":"+pad(minute)+"\n");
osw.close();
} catch (Exception e) {
//Debug
//Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);
}
TextView mostra_awake = (TextView) findViewById(R.id.awakeview);
mostra_awake.setText(readFile(awake_file));
}
};

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
// Dialog comes from 'off button'
return new TimePickerDialog(this,mTimeSetListener_off, 0, 0, true);
case 2:
// Dialog comes from 'on button'
return new TimePickerDialog(this,mTimeSetListener_on, 0, 0, true);
}
return null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
settings = getSharedPreferences("sleep_prefs", 0);
editor = settings.edit();
mostra_awake = (TextView) findViewById(R.id.awakeview);
mostra_sleep = (TextView) findViewById(R.id.sleepview);
dillunsView = (TextView) findViewById(R.id.mondayView);
dimartsView = (TextView) findViewById(R.id.tuesdayView);
dimecresView = (TextView) findViewById(R.id.wednesdayView);
dijousView = (TextView) findViewById(R.id.thursdayView);
divendresView = (TextView) findViewById(R.id.fridayView);
dissabteView = (TextView) findViewById(R.id.saturdayView);
diumengeView = (TextView) findViewById(R.id.sundayView);
final Button button_off = (Button) findViewById(R.id.button_apagar);
final Button button_on = (Button) findViewById(R.id.button_encender);
final Button button_reset = (Button) findViewById(R.id.removealarms);
final Button button_dilluns = (Button) findViewById(R.id.button_monday);
final Button button_dimarts = (Button) findViewById(R.id.button_tuesday);
final Button button_dimecres = (Button) findViewById(R.id.button_wednesday);
final Button button_dijous = (Button) findViewById(R.id.button_thursday);
final Button button_divendres = (Button) findViewById(R.id.button_friday);
final Button button_dissabte = (Button) findViewById(R.id.button_saturday);
final Button button_diumenge = (Button) findViewById(R.id.button_sunday);
//final RadioButton radio_all = (RadioButton) findViewById(R.id.radio_all);
//final RadioButton radio_work = (RadioButton) findViewById(R.id.radio_work);
//radio_all.setOnClickListener(radio_listener_all);
//radio_work.setOnClickListener(radio_listener_work);
//SharedPreferences settings_read = getSharedPreferences("sleep_prefs", 0);
// reboot tindra el contingut de si s'ha rebotat o no
String reboot = settings.getString("reboot_string", "si");

if (reboot=="no") {
// en el cas que no s'hagi rebotat el sistema desde l'ultima execució
mostra_awake.setText(readFile(awake_file));
mostra_sleep.setText(readFile(sleep_file));
} else {
// en el cas que s'hagi rebotat el sistema desde l'ultima execució
deleteFile ("sleep_file");
deleteFile ("awake_file");
mostra_awake.setText("No awake time available");
mostra_sleep.setText("No sleep time available");
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings.edit();
editor.putString("reboot_string","no");
editor.putString("dilluns","no");
editor.putString("dimarts","no");
editor.putString("dimecres","no");
editor.putString("dijous","no");
editor.putString("divendres","no");
editor.putString("dissabte","no");
editor.putString("diumenge","no");
// Commit the edits!
editor.commit();
}

dilluns = settings.getString("dilluns", "no");
dimarts = settings.getString("dimarts", "no");
dimecres = settings.getString("dimecres", "no");
dijous = settings.getString("dijous", "no");
divendres = settings.getString("divendres", "no");
dissabte = settings.getString("dissabte", "no");
diumenge = settings.getString("diumenge", "no");
dillunsView.setText(dilluns);
dimartsView.setText(dimarts);
dimecresView.setText(dimecres);
dijousView.setText(dijous);
divendresView.setText(divendres);
dissabteView.setText(dissabte);
diumengeView.setText(diumenge);

button_dilluns.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (dilluns.equals("no")) {
//

editor.putString("dilluns","yes");
editor.commit();
dilluns="yes";
dillunsView.setText(dilluns);
} else {
//SharedPreferences settings = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dilluns","no");
editor.commit();
dilluns="no";
dillunsView.setText(dilluns);
}

}
});
button_dimarts.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (dimarts.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dimarts","yes");
editor.commit();
dimarts="yes";
dimartsView.setText(dimarts);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dimarts","no");
editor.commit();
dimarts="no";
dimartsView.setText(dimarts);
}

}
});

button_dimecres.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (dimecres.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dimecres","yes");
editor.commit();
dimecres="yes";
dimecresView.setText(dimecres);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dimecres","no");
editor.commit();
dimecres="no";
dimecresView.setText(dimecres);
}

}
});

button_dijous.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (dijous.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dijous","yes");
editor.commit();
dijous="yes";
dijousView.setText(dijous);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dijous","no");
editor.commit();
dijous="no";
dijousView.setText(dijous);
}

}
});

button_divendres.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (divendres.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("divendres","yes");
editor.commit();
divendres="yes";
divendresView.setText(divendres);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("divendres","no");
editor.commit();
divendres="no";
divendresView.setText(divendres);
}

}
});

button_dissabte.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (dissabte.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dissabte","yes");
editor.commit();
dissabte="yes";
dissabteView.setText(dissabte);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("dissabte","no");
editor.commit();
dissabte="no";
dissabteView.setText(dissabte);
}

}
});

button_diumenge.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (diumenge.equals("no")) {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("diumenge","yes");
editor.commit();
diumenge="yes";
diumengeView.setText(diumenge);
} else {
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("diumenge","no");
editor.commit();
diumenge="no";
diumengeView.setText(diumenge);
}

}
});

button_off.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open TimePicker for sound disable
showDialog(1);
}
});
button_on.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open TimePicker for sound enable
showDialog(2);
}
});
button_reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// borra les alarmes
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender_off);
am.cancel(sender_on);
// borra els arxius
deleteFile ("sleep_file");
deleteFile ("awake_file");
mostra_awake.setText("No awake time available");
mostra_sleep.setText("No sleep time available");
//SharedPreferences settings_write = getSharedPreferences("sleep_prefs", 0);
//SharedPreferences.Editor editor = settings_write.edit();
editor.putString("reboot_string","no");
// Commit the edits!
editor.commit();
}
});

}
}

My first app: Sleep Tight 1.3 beta

Hi! Here is my first app!

This simple but useful app allows you configure the system to enable and/or disable all sounds of your android device at specific time.

You can configure it to automatically disable all sounds before your sleep time, and reenable it when you want. Easy, this is it.

'Sleep Tight' app icon

‘Sleep Tight’ app icon

You can download for free from Google Play.