No time for comments...
authorgumartinm <gustavo@gumartinm.name>
Mon, 11 Jun 2012 05:22:06 +0000 (07:22 +0200)
committergumartinm <gustavo@gumartinm.name>
Mon, 11 Jun 2012 05:22:06 +0000 (07:22 +0200)
Android/MobiAds/AndroidManifest.xml
Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java
Android/MobiAds/src/de/android/mobiads/MobiAdsService.java
Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java
Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java
Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java

index f13b828..46df370 100644 (file)
@@ -58,7 +58,9 @@
             android:name=".list.MobiAdsNewAdsActivity"
             android:theme="@android:style/Theme.Black" 
             android:screenOrientation="portrait"
-            android:configChanges="touchscreen|keyboard" >
+            android:configChanges="touchscreen|keyboard"
+            android:noHistory="true"
+            android:taskAffinity="de.android.mobiads.list">
             <intent-filter>
                 <action android:name="android.intent.action.MOBIADSNEWADS" />
                 <category android:name="android.intent.category.DEFAULT" />
index 064e0cc..856f093 100644 (file)
@@ -1,6 +1,8 @@
 package de.android.mobiads;
 
 import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningServiceInfo;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -100,9 +102,11 @@ public class MobiAdsMainActivity extends Activity {
     
     
     public void onClickStartService(View v) {
-       Intent intent = new Intent(MobiAdsMainActivity.this, MobiAdsService.class);
-        intent.putExtra("cookie", MobiAdsMainActivity.cookie);
-       this.startService(intent);
+       if (!this.isMyServiceRunning()) {
+               Intent intent = new Intent(MobiAdsMainActivity.this, MobiAdsService.class);
+               intent.putExtra("cookie", MobiAdsMainActivity.cookie);
+               this.startService(intent);
+       }
     }
     
     public void onClickListLocalAds(View v) {
@@ -111,4 +115,14 @@ public class MobiAdsMainActivity extends Activity {
        intent.putExtra("login", false);
                this.startActivity(intent);
     }
+    
+    private boolean isMyServiceRunning() {
+        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
+            if (MobiAdsService.class.getName().equals(service.service.getClassName())) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
index d326621..6364c64 100644 (file)
@@ -120,11 +120,13 @@ public class MobiAdsService extends Service {
         CharSequence contentText;
         if ((noReadCount = this.mobiAdsBatch.noReadAdsCount()) == 0) {
                contentText = getText(R.string.remote_service_content_empty_notification);
+               showNotification(0, noReadCount, contentText, null);
         }
         else {
                contentText = getText(R.string.remote_service_content_notification);
+               showNotification(0, noReadCount, contentText, MobiAdsNewAdsActivity.class);
         }
-        showNotification(0, noReadCount, contentText);
+        
         return super.onStartCommand(intent, flags, startId);
     }
     
@@ -153,12 +155,16 @@ public class MobiAdsService extends Service {
        /**
      * Show a notification while this service is running.
      */
-    public void showNotification(final int level, final int noReadAds, CharSequence contentText) {        
-
-        Intent intent =  new Intent(this, MobiAdsNewAdsActivity.class);
-        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
-        // The PendingIntent to launch our activity if the user selects this notification
-        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+    public void showNotification(final int level, final int noReadAds, CharSequence contentText, Class<?> cls) {        
+       PendingIntent contentIntent = null;
+       
+       if (cls != null) {
+               Intent intent =  new Intent(this, MobiAdsNewAdsActivity.class);
+            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
+            // The PendingIntent to launch our activity if the user selects this notification
+            contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+       }
+        
                 
         // Set the icon, scrolling text and timestamp
         Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()).
index 06ff45a..4b59d62 100644 (file)
@@ -33,6 +33,7 @@ import android.net.http.AndroidHttpClient;
 import android.util.Log;
 import de.android.mobiads.MobiAdsService;
 import de.android.mobiads.R;
+import de.android.mobiads.list.MobiAdsNewAdsActivity;
 import de.android.mobiads.provider.Indexer;
 
 public class MobiAdsBatch {
@@ -129,7 +130,7 @@ public class MobiAdsBatch {
                                                                                                        getText(R.string.remote_service_content_notification);
                                                        }
                                                                ((MobiAdsService)MobiAdsBatch.this.context).
-                                                                               showNotification(0, noReadCount, contentText);
+                                                                               showNotification(0, noReadCount, contentText, MobiAdsNewAdsActivity.class);
 
                                                        } catch (Throwable e1) {
                                                                //In case of any error, remove the index database and the file
index 2879512..19d8447 100644 (file)
@@ -162,10 +162,6 @@ public class MobiAdsListActivity extends Activity {
     
     public void showNotification(final int level, final int noReadAds, CharSequence contentText) {        
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
-        Intent intent =  new Intent(this, MobiAdsNewAdsActivity.class);
-        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
-        // The PendingIntent to launch our activity if the user selects this notification
-        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                 
         // Set the icon, scrolling text and timestamp
         Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()).
@@ -174,8 +170,7 @@ public class MobiAdsListActivity extends Activity {
                                                                                                                setWhen(System.currentTimeMillis()).
                                                                                                                        setContentText(contentText).
                                                                                                                                setContentTitle(getText(R.string.remote_service_title_notification)).
-                                                                                                                                       setNumber(noReadAds).
-                                                                                                                                               setContentIntent(contentIntent);
+                                                                                                                                       setNumber(noReadAds);
         Notification notification = notificationBuilder.getNotification();
         notification.flags |= Notification.FLAG_NO_CLEAR;
 
index fd42e16..9dcaff6 100644 (file)
@@ -126,10 +126,6 @@ public class MobiAdsNewAdsActivity extends Activity {
            
            public void showNotification(final int level, final int noReadAds, CharSequence contentText) {        
                NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
-               Intent intent =  new Intent(this, MobiAdsNewAdsActivity.class);
-               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
-               // The PendingIntent to launch our activity if the user selects this notification
-               PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        
                // Set the icon, scrolling text and timestamp
                Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()).
@@ -138,8 +134,7 @@ public class MobiAdsNewAdsActivity extends Activity {
                                                                                                                        setWhen(System.currentTimeMillis()).
                                                                                                                                setContentText(contentText).
                                                                                                                                        setContentTitle(getText(R.string.remote_service_title_notification)).
-                                                                                                                                               setNumber(noReadAds).
-                                                                                                                                                       setContentIntent(contentIntent);
+                                                                                                                                               setNumber(noReadAds);
                Notification notification = notificationBuilder.getNotification();
                notification.flags |= Notification.FLAG_NO_CLEAR;