Android Open Source - android-sms-relay Alarm Receiver






From Project

Back to project page android-sms-relay.

License

The source code is released under:

GNU General Public License

If you think the Android project android-sms-relay listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/***
  Copyright (c) 2011 CommonsWare, LLC//w w w .j  a  v  a 2s  . c o m
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.cwac.wakeful;

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.XmlResourceParser;
import android.util.Log;
import com.commonsware.cwac.wakeful.WakefulIntentService.AlarmListener;

public class AlarmReceiver extends BroadcastReceiver {
  private final String WAKEFUL_META_DATA="com.commonsware.cwac.wakeful";
  
  @Override
  public void onReceive(Context ctxt, Intent intent) {
    AlarmListener listener=getListener(ctxt);
    
    if (listener!=null) {
      if (intent.getAction()==null) {
        SharedPreferences prefs=ctxt.getSharedPreferences(WakefulIntentService.NAME, 0);

        prefs
          .edit()
          .putLong(WakefulIntentService.LAST_ALARM, System.currentTimeMillis())
          .commit();
        
        listener.sendWakefulWork(ctxt);
      }
      else {
        WakefulIntentService.scheduleAlarms(listener, ctxt, true);
      }
    }
  }
  
  @SuppressWarnings("unchecked")
  private WakefulIntentService.AlarmListener getListener(Context ctxt) {
    PackageManager pm=ctxt.getPackageManager();
    ComponentName cn=new ComponentName(ctxt, getClass());
    
    try {
      ActivityInfo ai=pm.getReceiverInfo(cn,
                                         PackageManager.GET_META_DATA);
      XmlResourceParser xpp=ai.loadXmlMetaData(pm,
                                               WAKEFUL_META_DATA);
      
      while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
        if (xpp.getEventType()==XmlPullParser.START_TAG) {
          if (xpp.getName().equals("WakefulIntentService")) {
            String clsName=xpp.getAttributeValue(null, "listener");
            Class<AlarmListener> cls=(Class<AlarmListener>)Class.forName(clsName);
            
            return(cls.newInstance());
          }
        }
        
        xpp.next();
      }
    }
    catch (NameNotFoundException e) {
      Log.e(getClass().getName(), "Cannot find own info???", e);
    }
    catch (XmlPullParserException e) {
      Log.e(getClass().getName(), "Malformed metadata resource XML", e);
    }
    catch (IOException e) {
      Log.e(getClass().getName(), "Could not read resource XML", e);
    }
    catch (ClassNotFoundException e) {
      Log.e(getClass().getName(), "Listener class not found", e);
    }
    catch (IllegalAccessException e) {
      Log.e(getClass().getName(), "Listener is not public or lacks public constructor", e);
    }
    catch (InstantiationException e) {
      Log.e(getClass().getName(), "Could not create instance of listener", e);
    }
    
    return(null);
  }
}




Java Source Code List

com.commonsware.cwac.wakeful.AlarmReceiver.java
com.commonsware.cwac.wakeful.WakefulIntentService.java
com.nyaruka.android.actionbarcompat.ActionBarActivity.java
com.nyaruka.android.actionbarcompat.ActionBarHelperBase.java
com.nyaruka.android.actionbarcompat.ActionBarHelperHoneycomb.java
com.nyaruka.android.actionbarcompat.ActionBarHelperICS.java
com.nyaruka.android.actionbarcompat.ActionBarHelper.java
com.nyaruka.android.actionbarcompat.SimpleMenuItem.java
com.nyaruka.android.actionbarcompat.SimpleMenu.java
com.nyaruka.androidrelay.AlarmListener.java
com.nyaruka.androidrelay.AndroidRelay.java
com.nyaruka.androidrelay.BootStrapper.java
com.nyaruka.androidrelay.CheckService.java
com.nyaruka.androidrelay.MainActivity.java
com.nyaruka.androidrelay.MessageListFragment.java
com.nyaruka.androidrelay.RebootService.java
com.nyaruka.androidrelay.RelayService.java
com.nyaruka.androidrelay.SMSModem.java
com.nyaruka.androidrelay.SettingsActivity.java
com.nyaruka.androidrelay.data.TextMessageHelper.java
com.nyaruka.androidrelay.data.TextMessage.java
com.nyaruka.log.LogCollector.java