Example usage for android.telephony TelephonyManager CALL_STATE_IDLE

List of usage examples for android.telephony TelephonyManager CALL_STATE_IDLE

Introduction

In this page you can find the example usage for android.telephony TelephonyManager CALL_STATE_IDLE.

Prototype

int CALL_STATE_IDLE

To view the source code for android.telephony TelephonyManager CALL_STATE_IDLE.

Click Source Link

Document

Device call state: No activity.

Usage

From source file:Main.java

public static boolean isInSystemCall(Context context) {
    TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telManager == null)
        return false;
    return (telManager.getCallState() != TelephonyManager.CALL_STATE_IDLE);
}

From source file:com.neuron.fantecremote.PhoneIntentReciever.java

public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        //Log.d("DEBUG", "IDLE");
        break;//from   w  w  w . j  a  v a  2  s.com
    case TelephonyManager.CALL_STATE_OFFHOOK:
        //Log.d("DEBUG", "OFFHOOK");
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        //Log.d("DEBUG", "RINGING");
        String apiurl = Settings.GetApiUrl(prefs);
        if (apiurl != null && prefs.getBoolean("autopause", true)) {
            new SendCommandTask().execute(apiurl + "CMD_PAUSE");
        }
        break;
    }
}

From source file:com.goftagram.telegram.messenger.MusicPlayerService.java

@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    try {/*from   w ww. j  a va2s  . c  o  m*/
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    if (MediaController.getInstance()
                            .isPlayingAudio(MediaController.getInstance().getPlayingMessageObject())
                            && !MediaController.getInstance().isAudioPaused()) {
                        MediaController.getInstance()
                                .pauseAudio(MediaController.getInstance().getPlayingMessageObject());
                    }
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {

                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    super.onCreate();
}

From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioProgressDidChanged);
    NotificationManager.getInstance().addObserver(this, NotificationManager.audioPlayStateChanged);
    try {/* w  w  w.j ava2 s .c o m*/
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    if (MediaController.getInstance()
                            .isPlayingAudio(MediaController.getInstance().getPlayingSongDetail())
                            && !MediaController.getInstance().isAudioPaused()) {
                        MediaController.getInstance()
                                .pauseAudio(MediaController.getInstance().getPlayingSongDetail());
                    }
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {

                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    } catch (Exception e) {
        Log.e("tmessages", e.toString());
    }
    super.onCreate();
}

From source file:com.meiste.tempalarm.gcm.GcmIntentService.java

@Override
protected void onHandleIntent(final Intent intent) {
    final Bundle extras = intent.getExtras();
    final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    final String messageType = gcm.getMessageType(intent);

    if (extras != null && !extras.isEmpty() && extras.containsKey(MSG_KEY)
            && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
        final String type = extras.getString(MSG_KEY);
        final String state = extras.getString(STATE_KEY, "UNKNOWN");
        Timber.d("Received %s message with state %s", type, state);

        // Only show notification if user wants results notifications
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        final boolean ok2notify = prefs.getBoolean(AppConstants.PREF_NOTIFICATIONS, true);

        SyncAdapter.requestSync(this, ok2notify);

        if (ok2notify) {
            cancelNotification();//from   w w  w. j a  v  a  2 s.  c o  m

            final Intent killIntent = new Intent(AppConstants.INTENT_ACTION_KILL_ALARM);
            LocalBroadcastManager.getInstance(this).sendBroadcastSync(killIntent);

            final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            final boolean inCall = tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;

            switch (type) {
            case MSG_KEY_ALARM:
                handleAlarm(state, inCall);
                break;
            case MSG_KEY_SENSOR:
                handleSensor(state, inCall);
                break;
            default:
                Timber.i("Message type unknown. Ignoring.");
                break;
            }
        }
    }

    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

From source file:net.geniecode.ttr.ScheduleReceiver.java

@SuppressWarnings("deprecation")
@SuppressLint({ "Recycle", "NewApi", "InlinedApi" })
@Override//from  ww  w  .  j av a2s .c o m
public void onReceive(Context context, Intent intent) {
    if (!Schedules.SCHEDULE_ACTION.equals(intent.getAction())) {
        // Unknown intent, bail.
        return;
    }

    Schedule schedule = null;
    // Grab the schedule from the intent. Since the remote AlarmManagerService
    // fills in the Intent to add some extra data, it must unparcel the
    // Schedule object. It throws a ClassNotFoundException when unparcelling.
    // To avoid this, do the marshalling ourselves.
    final byte[] data = intent.getByteArrayExtra(Schedules.SCHEDULE_RAW_DATA);
    if (data != null) {
        Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        schedule = Schedule.CREATOR.createFromParcel(in);
    }

    if (schedule == null) {
        // Make sure we set the next schedule if needed.
        Schedules.setNextSchedule(context);
        return;
    }

    // Disable this schedule if it does not repeat.
    if (!schedule.daysOfWeek.isRepeatSet()) {
        Schedules.enableSchedule(context, schedule.id, false);
    } else {
        // Enable the next schedule if there is one. The above call to
        // enableSchedule will call setNextSchedule so avoid calling it twice.
        Schedules.setNextSchedule(context);
    }

    long now = System.currentTimeMillis();

    if (now > schedule.time + STALE_WINDOW) {
        return;
    }

    // Get telephony service
    mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    // Execute only for devices with versions of Android less than 4.2
    if (android.os.Build.VERSION.SDK_INT < 17) {
        // Get flight mode state
        boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;

        // Get Wi-Fi service
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Enable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Get Wi-Fi state and disable that one too, just in case
            // (On some devices it doesn't get disabled when the flight mode is
            // turned on, so we do it here)
            boolean isWifiEnabled = mWifiManager.isWifiEnabled();

            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);

            if (isWifiEnabled) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
                mWifiManager.setWifiEnabled(false);
            } else {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        } else if ((!schedule.aponoff) && (isEnabled) && (schedule.mode.equals("1"))) {
            // Disable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Restore previously remembered Wi-Fi state
            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
            Boolean WiFiState = settings.getBoolean(WIFI_STATE, true);

            if (WiFiState) {
                mWifiManager.setWifiEnabled(true);
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        }
        // Check whether there are ongoing phone calls, and if so
        // show notification instead of just enabling the flight mode
        else if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
        // Execute for devices with Android 4.2   or higher
    } else {
        // Get flight mode state
        String result = Settings.Global.getString(context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON);

        if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((!schedule.aponoff) && (result.equals("1")) && (schedule.mode.equals("1"))) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
    }

    // Get audio service
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    // Get current ringer mode and set silent or normal mode accordingly
    switch (mAudioManager.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        break;
    case AudioManager.RINGER_MODE_VIBRATE:
        // If the phone is set to vibrate let's make it completely silent
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        // or restore the regular sounds on it if that's scheduled
        else if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    }
}

From source file:com.smedic.tubtub.BackgroundAudioService.java

private void initPhoneCallListener() {
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override//from  ww  w  . j  a v  a2s .  co m
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                pauseVideo();
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                resumeVideo();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
            default:
                break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    };

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

From source file:com.pandoroid.PandoraRadioService.java

@Override
public void onCreate() {
    m_paused = false;//from  w ww. j  a v  a  2 s . c o  m
    m_pandora_remote = new PandoraRadio();
    image_downloader = new ImageDownloader();
    m_stations = new ArrayList<Station>();

    connectivity_manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    m_prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    // Register the listener with the telephony manager
    telephonyManager.listen(new PhoneStateListener() {
        boolean pausedForRing = false;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {

            case TelephonyManager.CALL_STATE_IDLE:
                if (pausedForRing && m_song_playback != null) {
                    if (m_prefs.getBoolean("behave_resumeOnHangup", true)) {
                        if (m_song_playback != null && !m_paused) {
                            m_song_playback.play();
                        }
                    }
                }

                pausedForRing = false;
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
            case TelephonyManager.CALL_STATE_RINGING:
                if (m_song_playback != null) {
                    m_song_playback.pause();
                }

                pausedForRing = true;
                break;
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);

    m_music_intent_receiver = new MusicIntentReceiver();
    this.registerReceiver(m_music_intent_receiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
}

From source file:za.co.neilson.alarm.alert.AlarmAlertActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(/*from  ww  w .  j av  a  2  s.c  o m*/
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.alarm_alert);

    Bundle bundle = this.getIntent().getExtras();
    alarm = (Alarm) bundle.getSerializable("alarm");

    sendNotification(this, alarm.getAlarmName(), alarm);

    this.setTitle(alarm.getAlarmName());

    switch (alarm.getDifficulty()) {
    case SUPEREASY:
        mathProblem = new MathProblem(1);
        break;
    case EASY:
        mathProblem = new MathProblem(3);
        break;
    case MEDIUM:
        mathProblem = new MathProblem(4);
        break;
    case HARD:
        mathProblem = new MathProblem(5);
        break;
    }

    answerString = String.valueOf(mathProblem.getAnswer());
    if (answerString.endsWith(".0")) {
        answerString = answerString.substring(0, answerString.length() - 2);
    }

    problemView = (TextView) findViewById(R.id.textView1);
    problemView.setText(mathProblem.toString());

    answerView = (TextView) findViewById(R.id.textView2);
    answerView.setText("= ?");

    ((Button) findViewById(R.id.Button0)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button1)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button2)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button3)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button4)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button5)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button6)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button7)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button8)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button9)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button_clear)).setOnClickListener(this);
    final AlarmAlertActivity foo = this;
    ((Button) findViewById(R.id.Button_alarm_off)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            alarmActive = false; //Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
            foo.finish();
        }
    });
    ((Button) findViewById(R.id.Button_decimal)).setOnClickListener(this);
    ((Button) findViewById(R.id.Button_minus)).setOnClickListener(this);

    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(getClass().getSimpleName(), "Incoming call: " + incomingNumber);
                try {
                    mediaPlayer.pause();
                } catch (IllegalStateException e) {

                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d(getClass().getSimpleName(), "Call State Idle");
                try {
                    mediaPlayer.start();
                } catch (IllegalStateException e) {

                }
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    // Toast.makeText(this, answerString, Toast.LENGTH_LONG).show();

    startAlarm();

}