package de.kanalmeister.presentation;
import ...
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] deviceID = {"***********", "***************"}; //IMEI von testgerät (**********: *************)
int i = 0;
boolean testgeraet = false;
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
Calendar cal3 = Calendar.getInstance();
Calendar cal4 = Calendar.getInstance();
Calendar cal5 = Calendar.getInstance();
cal2.set(2014, Calendar.MAY, 8, 14, 0, 0);
cal3.set(2014, Calendar.MAY, 8, 15, 35, 0);
cal4.set(2014, Calendar.MAY, 15, 14, 0, 0);
cal5.set(2014, Calendar.MAY, 15, 15, 35, 0);
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
setContentView(R.layout.activity_main); //layout-datei festlegen
for (i = 0; i < deviceID.length; i++)
{
testgeraet = tMgr.getDeviceId().equals(deviceID[i]); //IMEI mit IMEI von zugelassenen testgeräten vergleichen
}
if (testgeraet)
{
start(true);
}
else
{
if (((cal3.getTimeInMillis() >= cal.getTimeInMillis()) && (cal.getTimeInMillis() >= cal2.getTimeInMillis()))
|| ((cal5.getTimeInMillis() >= cal.getTimeInMillis()) && (cal.getTimeInMillis() >= cal4.getTimeInMillis()))) //aktuelles datum mit präsentationszeitraum vergleichen.
{
start(false);
}
else
{
if((cal5.getTimeInMillis() + 600000) < cal.getTimeInMillis()) //prüfen ab präsentation bereits vorbei
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setVisibility(TextView.VISIBLE);
textView.setText("Die Präsentation ist vorbei. Die Urheberrechtlich geschützten Daten werden nun gelöscht. Die App ist danach unbrauchbar und sollte deinstalliert werden.");
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(delete("android.resource://" + getPackageName() + "/" + R.raw.trailer)) //wenn löschen erfolgreich
{
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivity(intent); //nutzer zum deinstallations-screen weiterleiten
}
else
{
createError(102);
textView.setText("Die Daten konnten nicht automatisch gelöscht werden. Deinstalliere die App bitte manuell. Wenn du die Daten aus der App kopierst, handelst du Rechtswidrig. Dies kann Straf- und Zivilrechtlich verfolgt werden.");
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivity(intent); //nutzer zum deinstallations-screen weiterleiten
}
}
else
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setVisibility(TextView.VISIBLE);
textView.setText("Inhalt gesperrt! Bitte versuche es am 08.05.2014 oder 15.05.2014 zwischen 14:00-15:35 Uhr erneut.");
File file = new File("android.resource://" + getPackageName() + "/" + R.raw.trailer);
if (!file.setReadable(false)) //dateien vor zugriff schützen
{
createError(101);
}
}
}
}
}
public void onPause()
{
//startService(new Intent(this, notifyService.class));
}
public boolean delete(String path) //datei löschen
{
File file = new File(path);
return file.delete();
}
public void start(boolean tester)
{
File file = new File("android.resource://" + getPackageName() + "/" + R.raw.trailer);
if (!file.setReadable(true)) //zugriff erlauben
{
createError(101);
}
final VideoView videoView = (VideoView) findViewById(R.id.surface_view);
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.trailer);
videoView.start(); //video starten
int score = 0;
postData(getUserName(tester), score, false); //highscore senden
//Intent i = new Intent(this, SettingsActivity.class);
//startActivity(i);
}
public String getUserName(boolean tester)
{
AccountManager am = AccountManager.get(this); // "this" references the current Context
Account[] accounts = new Account[0];
if (am != null)
{
accounts = am.getAccountsByType("com.google");
}
if (tester)
{
return "TESTER_" + accounts[0].name;
}
else
{
return accounts[0].name;
}
}
public void postData(String userName, int score, boolean error) //highscore senden
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost;
String name1="username";
String name2;
if (error)
{
httppost = new HttpPost("http://testserver.info.cm/error_write.php");
name2="errorcode";
}
else
{
httppost = new HttpPost("http://testserver.info.cm/highscore_write.php");
name2="score";
}
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair(name1, userName));
nameValuePairs.add(new BasicNameValuePair(name2, String.valueOf(score)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
createError(201);
} catch (IOException e) {
createError(202);
}
}
public void createError(int errorcode)
{
Toast err = Toast.makeText(this, "Fehler " + String.valueOf(errorcode) + ". Die Daten werden automatisch an den Entwickler übermittelt.", Toast.LENGTH_SHORT);
err.show();
postData(getUserName(false), errorcode, true);
}
}
和我的androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.kanalmeister.presentation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="de.kanalmeister.presentation.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="de.kanalmeister.presentation.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<service android:enabled="true" android:name=".notifyService" />
</application>
</manifest>