biotinker / LibreTasks

An updated form of Omnidroid
Apache License 2.0
84 stars 22 forks source link

Omnidroid On/Off Setting #67

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Omnidroid runs a background service for monitoring.  The UI should provide
a way to turn this on/off essentially enabling/disabling Omnidroid in the
event that the user wants to quickly enable/disable all current rules (and
still have them maintain in the state they were in) or if they want to
disable it simply to save power.

Original issue reported on code.google.com by case.and...@gmail.com on 20 May 2010 at 5:09

GoogleCodeExporter commented 8 years ago

Original comment by eligeler...@gmail.com on 1 Jun 2010 at 6:25

GoogleCodeExporter commented 8 years ago
Should be displayed on the Settings Activity that will be forthcoming.  If you 
implement this on the main activity until the Settings Activity is done, that's 
fine.

Original comment by case.and...@gmail.com on 8 Jun 2010 at 11:03

GoogleCodeExporter commented 8 years ago
I want to do this by disabling the BCReciever. However, since the Reciever is 
created throught the Manifest file I believe it can't be disabled. Can I change 
how it is created (not in the Manifest file, but upon startup).

Original comment by eligeler...@gmail.com on 18 Jun 2010 at 9:18

GoogleCodeExporter commented 8 years ago
That would be fine if you want to enable/disable BCReceiver dynamically.  Just 
make sure funcations properly on boot.  Alternatively you could still have 
BCReceiver running, but have a condition statement in the onReceive() function 
that checks the SharedPreferences and exits if Omnidroid is disabled.

Original comment by case.and...@gmail.com on 19 Jun 2010 at 12:11

GoogleCodeExporter commented 8 years ago
I also think it is plausible since the Context API provided us with 
registerReceiver() and unregisterReceiver(), and the existing Starter class 
already has a start a service on boot functionality. I just also want to let 
you know about an issue I am working on - Issue 84, where it looks like Starter 
does not always receive android.intent.action.BOOT_COMPLETED, I am still not 
sure if this is also true on the actual device, but it happens quite often on 
my emulator (or does it has something to do with my ancient laptop?)

Original comment by renc...@gmail.com on 19 Jun 2010 at 4:19

GoogleCodeExporter commented 8 years ago
By the way, you also mentioned in the class about saving batteries, so I think 
stopping the EventMonitoringService is also a good idea, especially since it is 
listening for the system TIME_TICK event, which will arrive every minute.

Original comment by renc...@gmail.com on 19 Jun 2010 at 4:21

GoogleCodeExporter commented 8 years ago

Original comment by case.and...@gmail.com on 22 Jun 2010 at 8:37

GoogleCodeExporter commented 8 years ago
I just submitted a code review for Issue #19, and then I'd like to take a look 
at Issue #73 which happens to be blocked by this issue.  What's the status of 
this issue?  Thanks!

Original comment by case.and...@gmail.com on 28 Jun 2010 at 4:42

GoogleCodeExporter commented 8 years ago
Having trouble starting the BCreceiver locally. I have this code
      BCReceiver localReceiver = new BCReceiver();
      IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
      context.registerReceiver(localReceiver, filter);
and it is not working.
Also I need to add multiple intent filters. Do I need to register each one 
separately?

Original comment by eligeler...@gmail.com on 29 Jun 2010 at 11:54

GoogleCodeExporter commented 8 years ago
Before the major code overhaul (see r340 for where I grabbed this code) we had 
this working.  This will need to be re-written a little with better 
documentation, but in the AndroidManifest.xml we had these entries:

        <!--  Service/receiver that runs to catch events -->
        <service android:name=".bkgservice.BRService" />
        <receiver android:name=".bkgservice.BCReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
        <!--  Service/receiver that runs to catch events -->
        <service android:name=".bkgservice.BRService" />
        <receiver android:name=".bkgservice.BCReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <!--  We need to start our service on bootup -->
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <receiver android:name=".bkgservice.Starter">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
            <intent-filter>
                <action android:name="OmniRestart"/>
            </intent-filter>
        </receiver>

The corresponding classes were:

/*Broadcast Receiver to detect system bootup*/
public class Starter extends BroadcastReceiver {
        private ComponentName comp = null;
        private ComponentName service = null;

        public void onReceive(Context context, Intent intent) {
                // If we're booting up, start our service
                if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()) ||
                                "OmniStart".equals(intent.getAction())) {
                        comp = new ComponentName(context.getPackageName(), BRService.class
                                        .getName());
                        service = context.startService(new Intent().setComponent(comp));
                        if (null == service) {
                                Toast.makeText(context, "Failed to start OmniDroid Service", 5)
                                                .show();
                                Log.i("Starter", "Could not start service " + comp.toString());
                                OmLogger.write(context, "Starter could not start Service");
                        }
                } else if ("OmniRestart".equals(intent.getAction())) {
                        comp = new ComponentName(context.getPackageName(), BRService.class
                                        .getName());
                        context.stopService(new Intent().setComponent(comp));
                        service = context.startService(new Intent().setComponent(comp));
                        if (null == service) {
                                Toast.makeText(context, "Failed to start OmniDroid Service", 5)
                                                .show();
                                Log.i("Starter", "Could not start service " + comp.toString());
                                OmLogger.write(context, "Starter could not start Service");
                        }
                }
        }
}

// TODO (pradeep): Document this class
public class BRService extends Service {
  /*
   * (non-Javadoc)
   * 
   * @see android.app.Service#onBind(android.content.Intent)
   */
  IntentFilter Ifilter = new IntentFilter();
  BroadcastReceiver BR = new BCReceiver();

  @Override
  public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate() {
    try {

      // Initializing the UGParser. To be deleted from this Code after Andrews Module
      UGParser ug = new UGParser(getApplicationContext());

      /* Check the User Config to start OmniDroid */
      //String Enabled = ug.readLine("Enabled");
      //if (Enabled.equalsIgnoreCase("True")) {
        Toast.makeText(getBaseContext(), "Starting OmniDroid", 5).show();

        // Get the User Instances in an Arraylist from the User Config
        ArrayList<HashMap<String, String>> UCRecords = ug.readRecords();
        Iterator<HashMap<String, String>> i = UCRecords.iterator();
        while (i.hasNext()) {
          HashMap<String, String> HM1 = i.next();
          // Configure the Intent Filter with the Events if Instance in enabled
          if (HM1.get("EnableInstance").equalsIgnoreCase("True"))
            {
            Ifilter.addAction(HM1.get("EventName"));
            Toast.makeText(getBaseContext(), "REGISTERING: "+HM1.get("EventName"), 5).show();

            }
        }
        registerReceiver(BR, Ifilter);
      //} else {
        //Toast.makeText(getBaseContext(), "Stopping OmniDroid", 5).show();
        //unregisterReceiver(BR);
      //}
    } catch (Exception e) {
      Log.i("BRService", e.getLocalizedMessage());
      Log.i("BRService", e.toString());
      OmLogger.write(getApplicationContext(), "Not able to Enable/Diable Omnidroid");
      // Logger.write("Unable to start BroadcastReceiver");
    }
  }

  @Override
public boolean stopService(Intent name) {
    Toast.makeText(getBaseContext(), "Stopping OmniDroid", 5).show();
        return super.stopService(name);
}

/*
   * (non-Javadoc)
   * 
   * @see android.app.Service#onDestroy()
   */
  @Override
  public void onDestroy() {
    Toast.makeText(getBaseContext(), "Stopping OmniDroid", 5).show();
    super.onDestroy();

  }
}

public class BCReceiver extends BroadcastReceiver {

        //Context context;
        @Override
        public void onReceive(Context context, Intent intent)
            {
                //this.context = context;
                //Toast.makeText(context,"Caught by Broadcast Receiver",Toast.LENGTH_LONG).show();
                try
                        {
                        intent.setClass(context, edu.nyu.cs.omnidroid.core.DummyActivity.class);
                        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
                        String intentaction = intent.getAction();
                        //Toast.makeText(context,getLastCallLogEntry(context),Toast.LENGTH_LONG).show();
                context.startActivity(intent);

                Log.i("Received Intent", intent.getAction());
                }catch(Exception e)
                {
                        Log.i("Exception in Intent",e.getLocalizedMessage());
                        OmLogger.write(context,"Unable to execute required action");
                }
                }

        public void readSMS(Context context, Intent intent)
        {
                Bundle bundle = intent.getExtras();
            Toast.makeText(context, intent.getAction() , Toast.LENGTH_SHORT).show();
            SmsMessage[] msgs = null;
            String str = "";
            if (bundle != null) {
              Object[] pdus = (Object[])bundle.get("pdus");
              msgs = new SmsMessage[pdus.length];
              for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
              }
              Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

            }
            }

        private String getLastCallLogEntry( Context context ) {
                  String[] projection = new String[] {
                BaseColumns._ID,
                CallLog.Calls.NUMBER,
                        CallLog.Calls.TYPE
                  };
                  ContentResolver resolver = context.getContentResolver();
              Cursor cur = resolver.query( 
                                                CallLog.Calls.CONTENT_URI,
                            projection, 
                            null,
                                                null,
                            CallLog.Calls.DEFAULT_SORT_ORDER );
              int numberColumn = cur.getColumnIndex( CallLog.Calls.NUMBER ); 
              int typeColumn = cur.getColumnIndex( CallLog.Calls.TYPE );
                  if( !cur.moveToNext()) {
                        cur.close();
                        return "";
                  }
                  String number = cur.getString( numberColumn );
              String type = cur.getString( typeColumn );
                  String dir = null;
                  try {
                        int dircode = Integer.parseInt( type );
                        switch( dircode ) {
                          case CallLog.Calls.OUTGOING_TYPE:
                                        dir = "OUTGOING";
                                        break;

                          case CallLog.Calls.INCOMING_TYPE:
                                        dir = "INCOMING";
                                        break;

                          case CallLog.Calls.MISSED_TYPE:
                                        dir = "MISSED";
                                        break;
                        }
              } catch( NumberFormatException ex ) {}
                  if( dir == null )
                                dir = "Unknown, code: "+type;
                  cur.close();
                  return dir+","+number;
            }

}

As for your question about filters, I believe the answer is yes, did you look 
at the AndroidManifest.xml to see how we're doing it now?  BCReceiver already 
has a bunch of filters registered to it.

Original comment by case.and...@gmail.com on 30 Jun 2010 at 5:04

GoogleCodeExporter commented 8 years ago
Link for browsing the old revision I mentioned: 
http://code.google.com/p/omnidroid/source/browse/?r=340#svn/trunk/src

Original comment by case.and...@gmail.com on 30 Jun 2010 at 5:07

GoogleCodeExporter commented 8 years ago

Original comment by case.and...@gmail.com on 1 Jul 2010 at 11:15

GoogleCodeExporter commented 8 years ago
I am still having trouble understanding what this old code helps me for this 
issue. How will this help me turn off the BCReceiver?

Original comment by eligeler...@gmail.com on 5 Jul 2010 at 10:18

GoogleCodeExporter commented 8 years ago
Looks to me like these two lines do it (yes/no?):

comp = new ComponentName(context.getPackageName(), BRService.class.getName())
context.stopService(new Intent().setComponent(comp));

Original comment by case.and...@gmail.com on 6 Jul 2010 at 1:32

GoogleCodeExporter commented 8 years ago
eli,

are you still working on it? if not do you mind if I take over? this seems to 
be blocking i73. 

Original comment by sv767%ny...@gtempaccount.com on 12 Jul 2010 at 5:12

GoogleCodeExporter commented 8 years ago
You can take over.

Original comment by eligeler...@gmail.com on 12 Jul 2010 at 8:18

GoogleCodeExporter commented 8 years ago
Please review this at http://codereview.appspot.com/1775044/show

Original comment by sv767%ny...@gtempaccount.com on 12 Jul 2010 at 9:01

GoogleCodeExporter commented 8 years ago
committed r805

Original comment by sv767%ny...@gtempaccount.com on 13 Jul 2010 at 3:14