Пример #1
0
    /**
     *
     * This method provides the functionality of actually parsing
     *
     * through the service books on the handheld and determining
     *
     * which traffic routes are available based on that information.
     *
     * Before 4.2.0, this method is necessary to determine coverage.
     *
     */
    /*todomt
     * private static void parseServiceBooks()
     * {
     * // Add in our new items by scrolling through the ServiceBook API.
     * ServiceBook sb = ServiceBook.getSB();
     * ServiceRecord[] records = sb.findRecordsByCid( IPPP ); // The IPPP service represents the data channel for MDS and BIS-B
     * if( records == null ) {
     * return;
     * }
     * int numRecords = records.length;
     * for( int i = 0; i < numRecords; i++ ) {
     * ServiceRecord myRecord = records[i];
     * string name = myRecord.getName(); // Technically, not needed but nice for debugging.
     * string uid = myRecord.getUid(); // Technically, not needed but nice for debugging.
     * // First of all, the CID itself should be equal to IPPP if this is going to be an IPPP service book.
     * if( myRecord.isValid() && !myRecord.isDisabled() ) {
     *  // Now we need to determine if the service book is Desktop or BIS.  One could check against the
     *  // name but that is unreliable.  The best mechanism is to leverage the security of the service
     *  // book to determine the security of the channel.
     *  int encryptionMode = myRecord.getEncryptionMode();
     *  if( encryptionMode == ServiceRecord.ENCRYPT_RIM ) {
     *   _mdsSupport = true;
     *  } else {
     *   _bisSupport = true;
     *  }
     * }
     * }
     * if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
     * _wifiAvailable = true;
     * Logger.log("WIFI initially connected");
     * }
     * else {
     * Logger.log("WIFI initially not connected state= "+Integer.toString(WLANInfo.getWLANState()));
     * }
     * }
     */

    /**
     *
     * Returns the Connection object specified by the name (e.g. HttpConnection) using the
     *
     * appropriate transport mechanism (MDS, BIS-B, TCP) depending on what service books
     *
     * are currently supported on the handheld and using a priority scale in the following order:
     *
     * <code>
     *
     *      MDS
     *
     *      BIS-B
     *
     *      WAP - To be supported in the future
     *
     *      HTTP over Direct TCP
     *
     * </code>
     *
     * This method does NOT check for the name to ensure that HTTP is being requested and as such
     *
     * it may not work if you request a socket connection over the BIS-B transport protocol.
     *
     */
    public static string getConnectionSuffix(bool print)
    {
        string ConnectionSuffix = null;

        if (WIFI_ENABLED && _wifiAvailable)
        {
            ConnectionSuffix = ";interface=wifi";
        }
        else if (_bisSupport)
        {
            ConnectionSuffix = ";deviceside=false;ConnectionType=mds-public";
        }
        else if (_mdsSupport)
        {
            ConnectionSuffix = ";deviceside=false";
        }
        else if (_wapSupport)
        {
            ConnectionSuffix = "";
        }
        else
        {
            // HTTP over Direct TCP
            ConnectionSuffix = ";deviceside=true";
        }
        if (print)
        {
            UIWorker.addUIEventLog("Connection Suffix length is " + ConnectionSuffix.Length + " , " + _wifiAvailable + " , " + _bisSupport + " , " + _mdsSupport + " , " + _wapSupport);
        }
        return(ConnectionSuffix);
    }
Пример #2
0
        public static void updateSoundLevel(int offset)
        {
            sound_level += offset;
            if (sound_level > 100)
            {
                sound_level = 100;
            }
            else if (sound_level < 0)
            {
                sound_level = 0;
            }

            try
            {
                if (c_set_sound_level == 0)
                {
                    c_set_sound_level = CibylCallTable.getAddressByName("roadmap_main_set_sound_level");
                }

                UIWorker.addUIEvent(c_set_sound_level, sound_level, 0, 0, 0, false);
            }
            catch (Exception e)
            {
                UIWorker.addUIEventLog("Error in SoundMgr setVolume" + e);
                Logger.log("Exception: " + e.ToString());
            }
        }
Пример #3
0
        private void playNextList()
        {
            lock (sound_lists)
            {
                current_list_id = (current_list_id + 1) % MAX_LISTS;

                if (sound_lists[current_list_id] == null)
                {
                    /* nothing to play */
                    current_list_id = -1;
                }
            }
            if (current_list_id == -1)
            {
                return;
            }

            current_list_item = 0;
            current_list      = (SoundList)CRunTime.getRegisteredObject(sound_lists[current_list_id].Value);
            if ((current_list.streams == null) || (current_list.streams.Length != current_list.count))
            {
                current_list.streams = new Stream[current_list.count];
            }
            for (int i = 0; i < current_list.count; i++)
            {
                try
                {
                    if (_soundDir.Equals(""))
                    {
                        current_list.streams[i] = App.GetResourceStream(new Uri(current_list.list[i], UriKind.Relative)).Stream;
                    }
                    else
                    { // This is a downloaded
                        lock (sound_lists)
                        {
                            if (Syscalls.FileExists(_soundDir + "/" + current_list.list[i]))
                            {
                                current_list.streams[i] = Syscalls.GetFileStream(_soundDir + "/" + current_list.list[i], FileMode.Open);
                            }
                            else
                            {
                                Logger.log("Could not find sound file : " + _soundDir + "/" + current_list.list[i]);
                                UIWorker.addUIEventLog("Could not find sound file : " + _soundDir + "/" + current_list.list[i]);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.log("Error creating sound stream:" + current_list.list[i] + "excp :  " + e);
                    UIWorker.addUIEventLog("Error creating sound stream:" + current_list.list[i] + "excp :  " + e);
                }
            }

            playNextItem();
        }
Пример #4
0
 public static void printOSVersionToLog()
 {
     try
     {
         UIWorker.addUIEventLog(" OS version is : " + Environment.OSVersion);
         //              CodeModuleManager.getModuleVersion(CodeModuleManager.getModuleHandleForObject("")));
     }
     catch (Exception e)
     {
         UIWorker.addUIEventLog("Exception in printOSVersionToLog" + e.ToString());
     }
 }
Пример #5
0
    /*
     *
     * Called when moving returning from background to foreground
     *
     */
    public void activate()
    {
        UIWorker.addUIEventLog("INFO: User returned to foreground");
        //todomt uncomment

        /*  if(locale_before_background != null){
         * Locale.setDefaultInput(locale_before_background);
         * }*/
        if (idTimerBackgroundDialog != 0)
        {     // Background timer open - Cancel it.
            //todomt   UiApplication.getUiApplication().cancelInvokeLater ( idTimerBackgroundDialog );
            idTimerBackgroundDialog = 0;
        }
    }
Пример #6
0
    public void startRead(int input_id)
    {
        if (lock_object == null)
        {
            UIWorker.addUIEventLog("startRead with no connection!");
            Logger.log("startRead with no connection!");
            eof = true;
            Monitor.Pulse(lock_object);
        }

        this.input_id = input_id;
        lock (lock_object)
        {
            do_read = true;
            Monitor.Pulse(lock_object);
        }
    }
Пример #7
0
    public static Timer schedule(int index, int interval, int repeat)
    {
        Timer timer;

        try
        {
            TimerCtx ctx = new TimerCtx(index, interval);
            timer = new Timer(TimerCallback, ctx, interval, interval);//Timeout.Infinite);
        }
        catch (Exception e)
        {
            UIWorker.addUIEventLog("Exception in scheduling task" + e.ToString());
            Logger.log("Exception in scheduling task " + e.ToString());
            return(null);
        }
        return(timer);
    }
Пример #8
0
    /*
     *
     * Called when moving returning from background to foreground.
     *
     * Pressing end key sends to background. To avoid wasting battery when user is unaware,
     *
     * after SECONDS_IN_BACKGROUND_B4_DLG seconds, a confirm dialog willl pop up, telling the
     *
     * user that waze is in background, and advise him to exit. If he doesn't answer - ( since he isn't
     *
     * looking at phone), exit anyway.
     *
     */
    /*todomt
     * public void deactivate(){
     * UIWorker.addUIEventLog("INFO : User went to background");
     * locale_before_background = Locale.getDefaultInput();
     * FreemapMainScreen.revertToInitialLocale();
     * if ( isCharging(DeviceInfo.getBatteryStatus()) == 1 ){
     * Logger.log("Charging, so not adding the background check");
     * return; // if charging, no need to
     * }
     * Application app = Application.getApplication();
     * idTimerBackgroundDialog = app.invokeLater (
     *  new Thread()
     *  {
     *   public void run()
     *   {
     *    if(c_ticker_sound == 0){
     *     try {
     *      c_ticker_sound = CibylCallTable.getAddressByName("roadmap_sound_play_background_sound");
     *     } catch (Exception e) {
     *      UIWorker.addUIEventLog("Exception trying to get Cybil address for c_ticker_sound");
     *      FreemapApp.safe_exit();
     *     }
     *    }
     *    UIWorker.addUIEvent(c_ticker_sound, 0, 0, 0, 0, true);
     *    UiApplication.getApplication().requestForeground();
     *    UIWorker.addUIEventLog("INFO: Waited 10 minutes, asking user if he wants to keep waze open. ");
     *    if(idTimerBackgroundDialog!=0)
     *     UiApplication.getUiApplication().cancelInvokeLater(idTimerBackgroundDialog );
     * //todomt      stayInBackgroundDialog = new Dialog("Waze is still running in the background. To avoid excess battery consumption it will shut down in 30 seconds.",
     *      new string[]{"Exit waze", "Stay in background"},
     *      new int[]{Dialog.YES, Dialog.NO},
     *      Dialog.YES,
     *      new Bitmap(0,0));
     * //todomt      stayInBackgroundDialog.setDialogClosedListener(new DialogClosedListener(){
     *     public void dialogClosed(Dialog d,int dialogReturnValue){
     *      if (dialogReturnValue == Dialog.NO){
     *       UiApplication.getApplication().requestBackground();
     *      }
     *
     *      if (dialogReturnValue == Dialog.YES){
     *       UIWorker.addUIEventLog("INFO : User accepted advice to not run in background. Exit.");
     *       safe_exit();
     *      }
     *     }
     *    });
     *
     * //todomt      stayInBackgroundDialog.show();
     *    resInvokeLaterappBGExit = UiApplication.getUiApplication().invokeLater (
     *      new Runnable()
     *      {
     *       // will be called some time after user hasn't picked his choice in the dialog. exit!
     *       public void run()
     *       {
     *        if(resInvokeLaterappBGExit !=0){
     *         UiApplication.getUiApplication().cancelInvokeLater(resInvokeLaterappBGExit );
     *         resInvokeLaterappBGExit = 0;
     *        }
     */
    //todomt uncomment

    /*          if(stayInBackgroundDialog.isDisplayed()){
     *         UIWorker.addUIEventLog("INFO : Dialog confirm dialog open after waiting for answer, exitting");
     *         safe_exit();
     *        }*/
    /*todomt       }
     *    }, SECONDS_BACKGROUND_DIALOG*1000, false);
     *
     *  if(resInvokeLaterappBGExit == -1)
     *   UIWorker.addUIEventLog("NO MORE TIMERS - resInvokeLaterappBGExit = -1 ");
     * }
     *
     * }, SECONDS_IN_BACKGROUND_B4_DLG*1000, false); // ask user if he wants to exit app.
     *
     * if(idTimerBackgroundDialog == -1)
     * UIWorker.addUIEventLog("NO MORE TIMERS - idTimerBackgroundDialog = -1");
     *
     * }
     */
    /*
     *
     * Exit safely through roadmap_main, so user won't get error messages next time he quits.
     *
     */
    public static void safe_exit()
    {
        try
        {
            if (c_roadmap_main_exit == 0)
            {
                c_roadmap_main_exit = CibylCallTable.getAddressByName("roadmap_main_exit");
            }
            if (c_roadmap_main_exit != 0)
            {
                int c_sp = (CRunTime.memory.Length * 4) - 8;
                UIWorker.addUIEvent(c_roadmap_main_exit, 0, 0, 0, 0, true);
            }
        }
        catch (Exception t)
        {
            UIWorker.addUIEventLog("FreeMapMainScreen Safe Exit exception" + t.ToString());
            //   t.printStackTrace();
            throw;     //todomt
            //Environment.Exit(0);
        }
    }
Пример #9
0
    public void runNetLoop()
    {
        http_response_sync = new ManualResetEvent(false);
        resp = null;
        conn = null;

        //bool wait = true;
        //while (wait)
        //{
        //    lock (concurrent_conns_lock)
        //    {
        //        if (concurrent_conns <= 6)
        //        {
        //            wait = false;
        //            concurrent_conns++;
        //        }
        //    }
        //    if (wait)
        //        Thread.Sleep(1000);
        //}


        int registeredHandle = 0;

        try
        {
            lock_object = new object();
            conn        = (HttpWebRequest)WebRequest.Create(url);//todomt (HttpConnection)Connector.open(url);
            conn.AllowReadStreamBuffering = false;
            conn.AllowAutoRedirect        = true;

            //System.Net.ServicePointManager.Expect100Continue = false;

            if (method == 0)
            {
                conn.Method = "GET";
            }
            else
            {
                conn.Method = "POST";
            }

            if (updateTime != null && updateTime.Trim().Length > 0)
            {
                conn.Headers["IfModifiedSince"] = updateTime;
            }

            registeredHandle = CRunTime.registerObject(conn);
        }
        catch (Exception e)
        {
            quit = true;
            Logger.log(e.ToString());
            UIWorker.addUIEventLog("Async Net : Exception opening URL " + e.ToString());
        }

        UIWorker.addUIEventValid(c_do_async_connect_cb, registeredHandle, cb_addr, context, 0, false, this);
        if (quit)
        {
            return;
        }

        while (!quit)
        {
            lock (lock_object)
            {
                if (!do_read)
                {
                    try
                    {
                        Monitor.Wait(lock_object);
                    }
                    catch (SynchronizationLockException e)
                    {
                        Logger.log(e.ToString());
                    }
                    if (quit)
                    {
                        return;
                    }
                    if (!do_read)
                    {
                        continue;
                    }
                }
            }

            Dictionary <string, string> conn_props;
            if (Syscalls.connection_properties.TryGetValue(registeredHandle, out conn_props))
            {
                foreach (string key in conn_props.Keys)
                {
                    string value = conn_props[key];
                    if (key.Equals("Content-type"))
                    {
                        conn.ContentType = value;
                    }
                    else if (key.Equals("User-Agent"))
                    {
                        ((HttpWebRequest)(conn)).UserAgent = value;
                    }
                }
                Syscalls.connection_properties.Remove(registeredHandle);
            }

            try
            {
                if (Stream == null)
                {
                    resp = null;
                    Exception exp = null;
                    try
                    {
                        if (conn.Method == "POST")
                        {
                            http_response_sync.Reset();

                            byte[] buffer;
                            if (Syscalls.buffered_requests.TryGetValue(registeredHandle, out buffer))
                            {
                                conn.BeginGetRequestStream(delegate(IAsyncResult result)
                                {
                                    var request = (WebRequest)result.AsyncState;
                                    using (var str = request.EndGetRequestStream(result))
                                    {
                                        Syscalls.buffered_requests.Remove(registeredHandle);
                                        str.Write(buffer, 0, (int)buffer.Length);
#if DEBUG
                                        Logger.log("http put: " + System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length));
#endif
                                    }
                                    http_response_sync.Set();
                                }, conn);
                            }
                            else
                            {
                                http_response_sync.Set();
                            }


                            http_response_sync.WaitOne();
                        }

                        http_response_sync.Reset();

                        Logger.log("Start downloading " + method + " " + conn.RequestUri);
                        conn.BeginGetResponse(delegate(IAsyncResult result)
                        {
                            try
                            {
                                var request = (HttpWebRequest)result.AsyncState;
                                Logger.log("Downloading " + request.RequestUri);
                                resp = (HttpWebResponse)request.EndGetResponse(result);
                                Logger.log("Finish getting response " + request.RequestUri);
                                http_response_sync.Set();
                            }
                            catch (Exception we)
                            {
                                exp             = we;
                                int status_code = -1;
                                if (resp != null)
                                {
                                    status_code = (int)resp.StatusCode;
                                }

                                Logger.log("status code " + status_code);

                                if (we is WebException)
                                {
                                    WebException wwe = (WebException)we;
                                    Logger.log("status - " + wwe.Status);
                                    if (wwe.Response != null)
                                    {
                                        Logger.log(" url:" + wwe.Response.ResponseUri);
                                    }

                                    Logger.log(wwe.ToString());

                                    if (wwe.Data != null)
                                    {
                                        Logger.log(wwe.Data.ToString());
                                    }
                                }
                                else
                                {
                                    Logger.log(we.ToString());
                                }

                                if (we.InnerException != null)
                                {
                                    Logger.log("Inner exception " + we.InnerException.ToString());
                                }

                                if (resp != null)
                                {
                                    resp.Dispose();
                                }
                                resp = null;
                                http_response_sync.Set();
                            }
                        }, conn);
                    }
                    catch (Exception ioe)
                    {
                        int status_code = -1;
                        if (resp != null)
                        {
                            resp.Dispose();
                            status_code = (int)resp.StatusCode;
                        }
                        exp = ioe;
                        Logger.log("status code2 " + status_code + " " + ioe.ToString());
                        if (ioe.InnerException != null)
                        {
                            Logger.log("and inner exception " + ioe.InnerException.ToString());
                        }

                        resp = null;
                        http_response_sync.Set();
                    }

                    http_response_sync.WaitOne();

                    if (resp != null)
                    {
                        Stream = resp.GetResponseStream();
                        int    status          = (int)resp.StatusCode;
                        long   data_size       = resp.ContentLength;
                        string lastModifiedStr = resp.Headers["Last-Modified"];
                        Logger.log("Finish getting response stream for " + url);
                        //Logger.log("Java header, s is " + lastModifiedStr);

                        /*
                         * We need to send c a complete header string, so we fake it by creating the
                         * res string. More header fields can be added later on besides the content length and last-modified
                         *
                         */
                        string res = "HTTP/1.1 " + status + " OK\r\nContent-Length: " + data_size + "\r\n";
                        if (lastModifiedStr != null)
                        {
                            res += "Last-Modified:" + lastModifiedStr + "\r\n\r\n";
                        }
                        else
                        {
                            res += "\r\n";
                        }

                        buffer = new byte[4096];
                        byte[] res_bytes = Syscalls.StringToAscii(res);
                        res_bytes.CopyTo(buffer, 0);
                        buffer_len     = res_bytes.Length;
                        buffer_cur_ptr = 0;
                    }
                    else
                    {
                        UIWorker.addUIEventLog("Exception in async net read: " + exp);
                        eof  = true;
                        quit = true;
                        //lock (concurrent_conns_lock)
                        //{
                        //    concurrent_conns--;
                        //}

                        ////buffer = new byte[4096];
                        //string res = "HTTP/1.1 404 Not Found\r\n";
                        ///*byte[] res_bytes*/buffer = Syscalls.StringToAscii(res);
                        ////res_bytes.CopyTo(buffer, 0);
                        //buffer_len = /*res_bytes.Length;*/buffer.Length;
                        //buffer_cur_ptr = 0;
                        //do_read = false;
                    }
                }
                else
                {
                    if (buffer_cur_ptr == buffer_len)
                    {
                        buffer_len = Stream.Read(buffer, 0, buffer.Length);
                        if (buffer_len == 0)
                        {
                            eof = true;
                            Stream.Close();
                            //lock (concurrent_conns_lock)
                            //{
                            //    concurrent_conns--;
                            //}
                        }
                        buffer_cur_ptr = 0;
                    }
                }
            }
            catch (Exception e)
            {
                UIWorker.addUIEventLog("Exception in async net read: " + e.ToString());
                eof  = true;
                quit = true;
            }
            lock (lock_object)
            {
                do_read = false;
            }

            // Call read CB
            if (is_valid)
            {
                UIWorker.addUIEventValid(c_input_ready_cb, input_id, 0, 0, 0, false, this);
            }
        }
    }