Profile Switcher app suggestions

Put your problem here if it does not fit any of the other categories.

Profile Switcher app suggestions

Postby 1taplay » Mon Feb 13, 2012 7:30 pm

Hey all,

im relatively new to android development and currently, im learning android programming while doing small projects on my own...

one of the projects that i been wanting to undertake is a profile switcher application which will change the audio loudness based on date/time etc...i know i will need to create some sort of background service that will check the time/date and perform changes to the phone's audio manager based on users preference..

i was wondering if any one can guide me on what all i need? i have been searching for code sample/examples for quite sometime but haven't gotten any luck so far...

Thanks for all your input...
1taplay
Freshman
Freshman
 
Posts: 5
Joined: Mon Feb 13, 2012 7:22 pm

Top

Re: Profile Switcher app suggestions

Postby Phyll » Mon Feb 13, 2012 11:10 pm

Hi 1taplay,

I guess I would start with the preferences layout. How you or your users will enter the information that will control the sound. Some kind of daily schedule, a calendar? What will be the granularity of the control? How will the volume be indicated and set? Will you use real numbers as the system does or make them hi, medium and low or something.

I guess that's where I'd start. After that, having it check the time and see what it's supposed to be doing according to the schedule should be a piece of cake.

Hope that helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Profile Switcher app suggestions

Postby 1taplay » Sun Feb 26, 2012 11:32 pm

hi all,

Ok so i have gotten most of the layout done, i also have created a service...now im facing one issue, for which im trying to find an elegant way to handle the code. Im saving the user's preference for time range in string format from androids TimerPicker control, i need to check if the time that user sets falls within the current time... right now im starting to do a bunch of if/else to check the hour and minute separately, while doing this, im thinking that there must be a better way to handle this logic?

the if/else will most likely work for the hour and minutes but i have no idea of how to compare and tell if the AM/PM is in the range or the time in general is in the current time range...

any suggestions?

Thanks
1taplay
Freshman
Freshman
 
Posts: 5
Joined: Mon Feb 13, 2012 7:22 pm

Re: Profile Switcher app suggestions

Postby Phyll » Mon Feb 27, 2012 12:26 pm

Hi 1taplay,

I guess I would make a function to convert my time string using Calendar or something to return milliseconds. Then just compare the results for your from/to times with the current one.

Here's a couple of those methods from Calendar.

Calendar currentTime = Calendar.getInstance();
Calendar fromTime = Calendar.getInstance();
fromTime.set(year, month, day, hourOfDay, minute, second);

long time = currentTime.getTimeInMillis();

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Profile Switcher app suggestions

Postby 1taplay » Mon Feb 27, 2012 4:13 pm

Hey Phyll,

i tried doing what you mentioned but im still facing the same issue of forcing the code to recognize AM/PM...here is the code that i have so far:

the 'from time' is coming in with the following: hour:minute:AM/PM -- 8:59:AM in string format
the 'to time' is coming in with the following: hour:minute:AM/PM -- 4:59:PM in string format

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(fromAMPM.equals("AM")){
  2.         from.set(from.AM_PM, from.AM);
  3. }else{
  4.         from.set(from.AM_PM, from.PM);
  5. }
  6.  
  7. //dont care about the YEAR and MONTH, so set it to current MONTH and YEAR                                      
  8. from.set(rightNow.get(rightNow.YEAR), rightNow.get(rightNow.MONTH), dayOfWeek, fromHour, fromMinute);                  
  9.                                        
  10. if(toAMPM.equals("AM")){
  11.         to.set(to.AM_PM, to.AM);
  12. }else{
  13.         to.set(to.AM_PM, to.PM);
  14. }      
  15.  
  16. //dont care about the YEAR and MONTH, so set it to current MONTH and YEAR                      
  17. to.set(rightNow.get(rightNow.YEAR), rightNow.get(rightNow.MONTH), dayOfWeek, toHour, toMinute);
  18.  
  19. //this is just for me to see what got set:
  20. SimpleDateFormat df3 = new SimpleDateFormat("HH:mm aaa");  
  21. String formattedDate1 = df3.format(from.getTime());
  22. String formattedDate2 = df3.format(to.getTime());
  23.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

after all this processing:

formattedDate1 is returning: 08:59 AM
formattedDate2 is returning: 04:59 AM

which obviously isnt right....
1taplay
Freshman
Freshman
 
Posts: 5
Joined: Mon Feb 13, 2012 7:22 pm

Re: Profile Switcher app suggestions

Postby Phyll » Mon Feb 27, 2012 10:25 pm

Hi 1taplay,

I worked on that some. Here is some code:

Code: Select all
public void doTimes() {   
    String t1 = "8:59:AM";
   String t2 = "8:59:PM";
   Log.i("TIMES", makeLong(t1)+" "+makeLong(t2));
   SimpleDateFormat df3 = new SimpleDateFormat("HH:mm aaa");   
    Date d = new Date();
    d.setTime(makeLong(t1));
   String formattedDate1 = df3.format(d);
    d.setTime(makeLong(t2));
   String formattedDate2 = df3.format(d);
   Log.i("TIMES", formattedDate1+" "+formattedDate2);
}   
   
public long makeLong(String time) {
    String[] parts = time.split(":");        //should show hours minutes am/pm   
   int h = Integer.parseInt(parts[0]);      //get each part
   int m = Integer.parseInt(parts[1]);
   if (parts[2].contains("PM")) h = h + 12; //make 24hour time
   Calendar c = Calendar.getInstance();
   c.set(c.get(c.YEAR), c.get(c.MONTH), c.get(c.DAY_OF_WEEK), h, m);
   return c.getTimeInMillis();
   }//convert


Once you have these numbers as integers all you have to do is use < and > to find out whether your current time is bracketed by the to and from times. I might even keep them as integers when possible. You can work on making the makeLong function more elegant. I just hacked it up in a short time. Tried it. It works. The do times is the test method I used.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: Profile Switcher app suggestions

Postby 1taplay » Thu Mar 01, 2012 1:40 am

Hey Phyll,

Thanks for the reply and the chunk of code...actually shortly after i posted here, i went back n re-read the Calendar documentation on android's website...and i realized that i kept missing the part that the .set(...) took 24 hour and by me passing in the data i was passing in, it was dropping off the AM/PM due to invalid data...never-the-less, i still wasnt able to get the .set(...) to work despite few attempts...so what i ended up doing instead was to set the hour, minute, second separately...

i have that working so far, but i was wondering if you can provide input on this other issue i am having some trouble with...basically, i have this timer code, and im calculating the next time when the timer code is suppose to execute after the 'current time' falls within the user's saved profile 'from' and 'to' time ranges...

the issue is that the timer code is using a nextUpdateInterval variable that is being calculated each time a profile is found...but when the profile time spans over midnight into the next day im unable to find a proper formula to calculate the nextUpdateInterval...im still trying to figure out how to account/calculate time that spans over to the next day, and at this point im starting to think that im might be making this task overly complicated? any suggestions?

here is the chuck of code that im working with as of now:
Code: Select all
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
    //..... some code to convert user stored times into proper time formats
    //PM to PM
    if(isFromTimePM == true && isToTimePM == true){

    }//PM to AM
    else if(isFromTimePM == true && isToTimePM == false){
        if(rightNowDate.getTime() >= fromDate.getTime() && rightNowDate.getTime() >= toDate.getTime()){
            foundProfileIndex = i;
                i = profileArrayListSize;


            nextUpdateInterval = rightNowDate.getTime() - toDate.getTime(); //toDate.getTime() - fromDate.getTime();
        }                   
    }//AM to AM
    else if(isFromTimePM == false && isToTimePM == false){

    }//AM to PM
    else if(isFromTimePM == false && isToTimePM == true){

    }}, 0, nextUpdateInterval);


Thanks again!
1taplay
Freshman
Freshman
 
Posts: 5
Joined: Mon Feb 13, 2012 7:22 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests