Example usage for android.appwidget AppWidgetManager ACTION_APPWIDGET_UPDATE

List of usage examples for android.appwidget AppWidgetManager ACTION_APPWIDGET_UPDATE

Introduction

In this page you can find the example usage for android.appwidget AppWidgetManager ACTION_APPWIDGET_UPDATE.

Prototype

String ACTION_APPWIDGET_UPDATE

To view the source code for android.appwidget AppWidgetManager ACTION_APPWIDGET_UPDATE.

Click Source Link

Document

Sent when it is time to update your AppWidget.

Usage

From source file:fr.shywim.antoinedaniel.ui.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case SETTINGS_REQUEST_CODE:
        AppWidgetManager man = AppWidgetManager.getInstance(this);
        if (man != null) {
            int[] ids = man.getAppWidgetIds(new ComponentName(mAppContext, HomeScreenWidgetProvider.class));
            Intent updateIntent = new Intent();
            updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            updateIntent.putExtra(HomeScreenWidgetProvider.WIDGET_IDS_KEY, ids);
            this.sendBroadcast(updateIntent);
        }/*from  w  w  w.jav a  2s .  com*/

        if (resultCode == RESULT_CONNECT_GOOGLE_CODE) {
            mTaskFragment.connectPlayServices(findViewById(VIEW_ID_POPUPS));
        }
        break;

    case PURCHASE_REQUEST_CODE:
        if (resultCode == RESULT_OK) {
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String sku = jo.getString("productId");
                if (sku.equals(utils.SKU_DONATE)) {
                    PrefUtils.setUserDonated(this, true);
                    Toast.makeText(this, R.string.toast_purchase_thanks, Toast.LENGTH_LONG).show();
                    mAdView.destroy();
                    LinearLayout adLayout = (LinearLayout) findViewById(R.id.adLayout);
                    adLayout.setVisibility(View.GONE);
                }
            } catch (JSONException e) {
                Toast.makeText(mContext, R.string.error, Toast.LENGTH_SHORT).show();
            }
        }
        break;

    case BlindGameActivity.REQUEST_CODE:
        /*if (resultCode == RESULT_OK){
           selectItem(DrawerPosition.GAMES, data.getExtras());
        } else selectItem(DrawerPosition.GAMES, null);*/
        break;

    case 1001:
        if (resultCode == RESULT_OK) {
            mTaskFragment.connectPlayServices(findViewById(VIEW_ID_POPUPS));
        } else {
            PrefUtils.setRefusedGoogle(this, true);
        }
        break;
    }
}

From source file:ch.fixme.status.Widget.java

public static void UpdateAllWidgets(final Context ctxt) {
    AppWidgetManager man = AppWidgetManager.getInstance(ctxt);
    int[] ids = man.getAppWidgetIds(new ComponentName(ctxt, Widget.class));
    Intent ui = new Intent();
    ui.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    ui.putExtra(Widget.WIDGET_IDS, ids);
    ui.putExtra(Widget.WIDGET_FORCE, true);
    ctxt.sendBroadcast(ui);//from   w  w w .  ja v  a  2  s.c om
}

From source file:de.domjos.schooltools.helper.Helper.java

public static void sendBroadCast(Context context, Class cls) {
    Intent intent = new Intent(context, cls);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, cls));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    context.sendBroadcast(intent);/*from   ww w . j av a 2 s.  c o m*/
}

From source file:com.piusvelte.taplock.client.core.TapLockService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            if (state == BluetoothAdapter.STATE_ON) {
                if (mStartedBT) {
                    if (mUIInterface != null) {
                        try {
                            mUIInterface.setMessage("Bluetooth enabled");
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }/*w  ww  .  jav a2  s .  com*/
                    }
                    if ((mQueueAddress != null) && (mQueueState != null))
                        requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                    else if (mRequestDiscovery && !mBtAdapter.isDiscovering())
                        mBtAdapter.startDiscovery();
                    else if (mUIInterface != null) {
                        try {
                            mUIInterface.setBluetoothEnabled();
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setMessage("Bluetooth disabled");
                    } catch (RemoteException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
                stopThreads();
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // connect if configured
                String address = device.getAddress();
                for (JSONObject deviceJObj : mDevices) {
                    try {
                        if (deviceJObj.getString(KEY_ADDRESS).equals(address)) {
                            // if queued
                            mDeviceFound = (mQueueAddress != null) && mQueueAddress.equals(address)
                                    && (mQueueState != null);
                            break;
                        }
                    } catch (JSONException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            } else if (mRequestDiscovery && (mUIInterface != null)) {
                String unpairedDevice = TapLock
                        .createDevice(device.getName(), device.getAddress(), DEFAULT_PASSPHRASE).toString();
                try {
                    mUIInterface.setUnpairedDevice(unpairedDevice);
                } catch (RemoteException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (mDeviceFound) {
                requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                mDeviceFound = false;
            } else if (mRequestDiscovery) {
                mRequestDiscovery = false;
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setDiscoveryFinished();
                    } catch (RemoteException e) {
                        Log.e(TAG, e.toString());
                    }
                }
            }
        } else if (ACTION_TOGGLE.equals(action) && intent.hasExtra(EXTRA_DEVICE_ADDRESS)) {
            String address = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
            requestWrite(address, ACTION_TOGGLE, null);
        } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            // create widget
            if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (intent.hasExtra(EXTRA_DEVICE_NAME)) {
                    // add a widget
                    String deviceName = intent.getStringExtra(EXTRA_DEVICE_NAME);
                    for (int i = 0, l = mDevices.size(); i < l; i++) {
                        String name = null;
                        try {
                            name = mDevices.get(i).getString(KEY_NAME);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        if ((name != null) && name.equals(deviceName)) {
                            JSONObject deviceJObj = mDevices.remove(i);
                            JSONArray widgetsJArr;
                            if (deviceJObj.has(KEY_WIDGETS)) {
                                try {
                                    widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                                } catch (JSONException e) {
                                    widgetsJArr = new JSONArray();
                                }
                            } else
                                widgetsJArr = new JSONArray();
                            widgetsJArr.put(appWidgetId);
                            try {
                                deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            mDevices.add(i, deviceJObj);
                            TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                            break;
                        }
                    }
                }
                buildWidget(appWidgetId);
            } else if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
                int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null) {
                    for (int appWidgetId : appWidgetIds)
                        buildWidget(appWidgetId);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            Log.d(TAG, "delete appWidgetId: " + appWidgetId);
            for (int i = 0, l = mDevices.size(); i < l; i++) {
                JSONObject deviceJObj = mDevices.get(i);
                if (deviceJObj.has(KEY_WIDGETS)) {
                    JSONArray widgetsJArr = null;
                    try {
                        widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (widgetsJArr != null) {
                        boolean wasUpdated = false;
                        JSONArray newWidgetsJArr = new JSONArray();
                        for (int widgetIdx = 0, wdigetsLen = widgetsJArr
                                .length(); widgetIdx < wdigetsLen; widgetIdx++) {
                            int widgetId;
                            try {
                                widgetId = widgetsJArr.getInt(widgetIdx);
                            } catch (JSONException e) {
                                widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                                e.printStackTrace();
                            }
                            Log.d(TAG, "eval widgetId: " + widgetId);
                            if ((widgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                                    && (widgetId == appWidgetId)) {
                                Log.d(TAG, "skip: " + widgetId);
                                wasUpdated = true;
                            } else {
                                Log.d(TAG, "include: " + widgetId);
                                newWidgetsJArr.put(widgetId);
                            }
                        }
                        if (wasUpdated) {
                            try {
                                deviceJObj.put(KEY_WIDGETS, newWidgetsJArr);
                                mDevices.remove(i);
                                mDevices.add(i, deviceJObj);
                                TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE),
                                        mDevices);
                                Log.d(TAG, "stored: " + deviceJObj.toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    JSONArray widgetsJArr = new JSONArray();
                    try {
                        deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                        mDevices.remove(i);
                        mDevices.add(i, deviceJObj);
                        TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return START_STICKY;
}

From source file:me.kartikarora.transfersh.activities.TransferActivity.java

public void updateWidgets() {
    Intent intent = new Intent(TransferActivity.this, FilesAppWidgetProvider.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(getApplicationContext());
    int[] ids = widgetManager
            .getAppWidgetIds(new ComponentName(getApplicationContext(), FilesAppWidgetProvider.class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);//w w w.  jav a2s  . c  o m
}

From source file:com.piusvelte.taplock.client.core.TapLockSettings.java

@Override
protected void onResume() {
    super.onResume();
    Intent intent = getIntent();//from  w  w  w  .jav  a  2 s . c  o  m
    if (mInWriteMode) {
        if (intent != null) {
            String action = intent.getAction();
            if (mInWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
                    && intent.hasExtra(EXTRA_DEVICE_NAME)) {
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                String name = intent.getStringExtra(EXTRA_DEVICE_NAME);
                if ((tag != null) && (name != null)) {
                    // write the device and address
                    String lang = "en";
                    // don't write the passphrase!
                    byte[] textBytes = name.getBytes();
                    byte[] langBytes = null;
                    int langLength = 0;
                    try {
                        langBytes = lang.getBytes("US-ASCII");
                        langLength = langBytes.length;
                    } catch (UnsupportedEncodingException e) {
                        Log.e(TAG, e.toString());
                    }
                    int textLength = textBytes.length;
                    byte[] payload = new byte[1 + langLength + textLength];

                    // set status byte (see NDEF spec for actual bits)
                    payload[0] = (byte) langLength;

                    // copy langbytes and textbytes into payload
                    if (langBytes != null) {
                        System.arraycopy(langBytes, 0, payload, 1, langLength);
                    }
                    System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
                    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT,
                            new byte[0], payload);
                    NdefMessage message = new NdefMessage(
                            new NdefRecord[] { record, NdefRecord.createApplicationRecord(getPackageName()) });
                    // Get an instance of Ndef for the tag.
                    Ndef ndef = Ndef.get(tag);
                    if (ndef != null) {
                        try {
                            ndef.connect();
                            if (ndef.isWritable()) {
                                ndef.writeNdefMessage(message);
                            }
                            ndef.close();
                            Toast.makeText(this, "tag written", Toast.LENGTH_LONG).show();
                        } catch (IOException e) {
                            Log.e(TAG, e.toString());
                        } catch (FormatException e) {
                            Log.e(TAG, e.toString());
                        }
                    } else {
                        NdefFormatable format = NdefFormatable.get(tag);
                        if (format != null) {
                            try {
                                format.connect();
                                format.format(message);
                                format.close();
                                Toast.makeText(getApplicationContext(), "tag written", Toast.LENGTH_LONG);
                            } catch (IOException e) {
                                Log.e(TAG, e.toString());
                            } catch (FormatException e) {
                                Log.e(TAG, e.toString());
                            }
                        }
                    }
                    mNfcAdapter.disableForegroundDispatch(this);
                }
            }
        }
        mInWriteMode = false;
    } else {
        SharedPreferences sp = getSharedPreferences(KEY_PREFS, MODE_PRIVATE);
        onSharedPreferenceChanged(sp, KEY_DEVICES);
        // check if configuring a widget
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                final String[] displayNames = TapLock.getDeviceNames(mDevices);
                final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                    mDialog = new AlertDialog.Builder(TapLockSettings.this).setTitle("Select device for widget")
                            .setItems(displayNames, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // set the successful widget result
                                    Intent resultValue = new Intent();
                                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                                    setResult(RESULT_OK, resultValue);

                                    // broadcast the new widget to update
                                    JSONObject deviceJObj = mDevices.get(which);
                                    dialog.cancel();
                                    TapLockSettings.this.finish();
                                    try {
                                        sendBroadcast(TapLock
                                                .getPackageIntent(TapLockSettings.this, TapLockWidget.class)
                                                .setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
                                                .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
                                                .putExtra(EXTRA_DEVICE_NAME, deviceJObj.getString(KEY_NAME)));
                                    } catch (JSONException e) {
                                        Log.e(TAG, e.toString());
                                    }
                                }
                            }).create();
                    mDialog.show();
                }
            }
        }
        // start the service before binding so that the service stays around for faster future connections
        startService(TapLock.getPackageIntent(this, TapLockService.class));
        bindService(TapLock.getPackageIntent(this, TapLockService.class), this, BIND_AUTO_CREATE);

        int serverVersion = sp.getInt(KEY_SERVER_VERSION, 0);
        if (mShowTapLockSettingsInfo && (mDevices.size() == 0)) {
            if (serverVersion < SERVER_VERSION)
                sp.edit().putInt(KEY_SERVER_VERSION, SERVER_VERSION).commit();
            mShowTapLockSettingsInfo = false;
            Intent i = TapLock.getPackageIntent(this, TapLockInfo.class);
            i.putExtra(EXTRA_INFO, getString(R.string.info_taplocksettings));
            startActivity(i);
        } else if (serverVersion < SERVER_VERSION) {
            // TapLockServer has been updated
            sp.edit().putInt(KEY_SERVER_VERSION, SERVER_VERSION).commit();
            mDialog = new AlertDialog.Builder(TapLockSettings.this).setTitle(R.string.ttl_hasupdate)
                    .setMessage(R.string.msg_hasupdate)
                    .setNeutralButton(R.string.button_getserver, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                            mDialog = new AlertDialog.Builder(TapLockSettings.this)
                                    .setTitle(R.string.msg_pickinstaller)
                                    .setItems(R.array.installer_entries, new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();
                                            final String installer_file = getResources()
                                                    .getStringArray(R.array.installer_values)[which];

                                            mDialog = new AlertDialog.Builder(TapLockSettings.this)
                                                    .setTitle(R.string.msg_pickdownloader)
                                                    .setItems(R.array.download_entries,
                                                            new DialogInterface.OnClickListener() {

                                                                @Override
                                                                public void onClick(DialogInterface dialog,
                                                                        int which) {
                                                                    dialog.cancel();
                                                                    String action = getResources()
                                                                            .getStringArray(
                                                                                    R.array.download_values)[which];
                                                                    if (ACTION_DOWNLOAD_SDCARD.equals(action)
                                                                            && copyFileToSDCard(installer_file))
                                                                        Toast.makeText(TapLockSettings.this,
                                                                                "Done!", Toast.LENGTH_SHORT)
                                                                                .show();
                                                                    else if (ACTION_DOWNLOAD_EMAIL
                                                                            .equals(action)
                                                                            && copyFileToSDCard(
                                                                                    installer_file)) {
                                                                        Intent emailIntent = new Intent(
                                                                                android.content.Intent.ACTION_SEND);
                                                                        emailIntent.setType(
                                                                                "application/java-archive");
                                                                        emailIntent.putExtra(Intent.EXTRA_TEXT,
                                                                                getString(
                                                                                        R.string.email_instructions));
                                                                        emailIntent.putExtra(
                                                                                Intent.EXTRA_SUBJECT,
                                                                                getString(R.string.app_name));
                                                                        emailIntent.putExtra(
                                                                                Intent.EXTRA_STREAM,
                                                                                Uri.parse("file://"
                                                                                        + Environment
                                                                                                .getExternalStorageDirectory()
                                                                                                .getPath()
                                                                                        + "/"
                                                                                        + installer_file));
                                                                        startActivity(Intent.createChooser(
                                                                                emailIntent, getString(
                                                                                        R.string.button_getserver)));
                                                                    }
                                                                }

                                                            })
                                                    .create();
                                            mDialog.show();
                                        }
                                    }).create();
                            mDialog.show();
                        }
                    }).create();
            mDialog.show();
        }
    }
}

From source file:com.songcode.materialnotes.ui.NoteEditActivity.java

private void updateWidget() {
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) {
        intent.setClass(this, NoteWidgetProvider_2x.class);
    } else if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_4X) {
        intent.setClass(this, NoteWidgetProvider_4x.class);
    } else {//from  ww w.j av a 2  s .  c o m
        Log.e(TAG, "Unspported widget type");
        return;
    }

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { mWorkingNote.getWidgetId() });

    sendBroadcast(intent);
    setResult(RESULT_OK, intent);
}

From source file:piuk.blockchain.android.WalletApplication.java

public void notifyWidgets() {
    final Context context = getApplicationContext();

    // notify widgets
    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    for (final AppWidgetProviderInfo providerInfo : appWidgetManager.getInstalledProviders()) {
        // limit to own widgets
        if (providerInfo.provider.getPackageName().equals(context.getPackageName())) {
            final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                    appWidgetManager.getAppWidgetIds(providerInfo.provider));
            context.sendBroadcast(intent);
        }/*from   w  w w. j  a  v  a2  s  . c  o m*/
    }
}

From source file:com.songcode.materialnotes.ui.NotesListActivity.java

private void updateWidget(int appWidgetId, int appWidgetType) {
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    if (appWidgetType == Notes.TYPE_WIDGET_2X) {
        intent.setClass(this, NoteWidgetProvider_2x.class);
    } else if (appWidgetType == Notes.TYPE_WIDGET_4X) {
        intent.setClass(this, NoteWidgetProvider_4x.class);
    } else {/* w w w  . ja va 2 s  .c  o  m*/
        Log.e(TAG, "Unspported widget type");
        return;
    }

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });

    sendBroadcast(intent);
    setResult(RESULT_OK, intent);
}

From source file:com.xperia64.timidityae.MusicService.java

@SuppressLint("NewApi")
public void play() {
    if (playList != null && currSongNumber >= 0) {
        shouldAdvance = false;/* w ww.j a  v  a 2  s  .  c  om*/
        death = true;
        fullStop = false;
        stop();
        death = false;
        Globals.shouldRestore = true;
        while (!death && ((Globals.isPlaying == 0 || JNIHandler.alternativeCheck == 333333))) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (!death) {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            String tmpTitle;
            Globals.currArt = null;
            final int songIndex;
            if (shuffleMode == 1) {
                songIndex = realSongNumber = shuffledIndices.get(currSongNumber);
            } else {
                songIndex = realSongNumber = currSongNumber;
            }
            try {
                mmr.setDataSource(playList.get(songIndex));
                tmpTitle = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
                if (tmpTitle != null) {
                    if (TextUtils.isEmpty(tmpTitle))
                        tmpTitle = playList.get(songIndex)
                                .substring(playList.get(songIndex).lastIndexOf('/') + 1);
                } else {
                    tmpTitle = playList.get(songIndex).substring(playList.get(songIndex).lastIndexOf('/') + 1);
                }

            } catch (RuntimeException e) {
                tmpTitle = playList.get(songIndex).substring(playList.get(songIndex).lastIndexOf('/') + 1);
            }
            boolean goodart = false;
            if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) // Please work
            {
                try {

                    byte[] art = mmr.getEmbeddedPicture();
                    if (art != null) {
                        Globals.currArt = BitmapFactory.decodeByteArray(art, 0, art.length);
                        goodart = Globals.currArt != null;
                    }
                } catch (Exception e) {
                }
            }
            if (!goodart) {
                String goodPath = playList.get(songIndex).substring(0,
                        playList.get(songIndex).lastIndexOf('/') + 1) + "folder.jpg";
                if (new File(goodPath).exists()) {
                    try {
                        Globals.currArt = BitmapFactory.decodeFile(goodPath);
                    } catch (RuntimeException e) {
                    }
                } else {
                    // Try albumart.jpg
                    goodPath = playList.get(songIndex).substring(0,
                            playList.get(songIndex).lastIndexOf('/') + 1) + "AlbumArt.jpg";
                    if (new File(goodPath).exists()) {
                        try {
                            Globals.currArt = BitmapFactory.decodeFile(goodPath);
                        } catch (RuntimeException e) {
                            // 
                        }
                    }
                }
            }
            if (shouldDoWidget) {
                Intent intent = new Intent(this, TimidityAEWidgetProvider.class);
                intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
                // since it seems the onUpdate() is only fired on that:
                ids = AppWidgetManager.getInstance(getApplication())
                        .getAppWidgetIds(new ComponentName(getApplication(), TimidityAEWidgetProvider.class));

                //intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                intent.putExtra("com.xperia64.timidityae.timidityaewidgetprovider.onlyart", true);
                sendBroadcast(intent);
            }
            Intent new_intent = new Intent();
            new_intent.setAction(getResources().getString(R.string.ta_rec));
            new_intent.putExtra(getResources().getString(R.string.ta_cmd), 6);
            sendBroadcast(new_intent);

            currTitle = tmpTitle;
            shouldAdvance = true;
            paused = false;

            final int x = JNIHandler.play(playList.get(songIndex));
            if (x != 0) {
                switch (x) {
                case -1:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.srv_fnf),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });

                    break;
                case -3:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Error initializing AudioTrack. Try decreasing the buffer size.",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                    break;
                case -9:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    getResources().getString(R.string.srv_loading), Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                default:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    String.format(getResources().getString(R.string.srv_unk), x),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                }

                Globals.isPlaying = 1;
                JNIHandler.type = true;
                shouldAdvance = false;
                JNIHandler.paused = false;
                stop();
            } else {
                updateNotification(currTitle, paused);
                new Thread(new Runnable() {
                    public void run() {
                        while (!death && ((Globals.isPlaying == 1 && shouldAdvance))) {
                            if (JNIHandler.alternativeCheck == 555555)
                                death = true;

                            //System.out.println(String.format("alt check: %d death: %s isplaying: %d shouldAdvance: %s seekBarReady: %s",JNIHandler.alternativeCheck,death?"true":"false",Globals.isPlaying,shouldAdvance?"true":"false",JNIHandler.seekbarReady?"true":"false"));
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (!death) {
                            Intent new_intent = new Intent();
                            new_intent.setAction(getResources().getString(R.string.ta_rec));
                            new_intent.putExtra(getResources().getString(R.string.ta_cmd), 0);
                            new_intent.putExtra(getResources().getString(R.string.ta_startt),
                                    JNIHandler.maxTime);
                            new_intent.putExtra(getResources().getString(R.string.ta_songttl), currTitle);
                            new_intent.putExtra(getResources().getString(R.string.ta_filename),
                                    playList.get(songIndex));
                            new_intent.putExtra("stupidNumber", songIndex);
                            sendBroadcast(new_intent);
                        }
                        if (new File(playList.get(songIndex) + ".def.tcf").exists()
                                || new File(playList.get(songIndex) + ".def.tzf").exists()) {
                            String suffix;
                            if (new File(playList.get(songIndex) + ".def.tcf").exists()
                                    && new File(playList.get(songIndex) + ".def.tzf").exists()) {
                                suffix = (Globals.compressCfg ? ".def.tzf" : ".def.tcf");
                            } else if (new File(playList.get(songIndex) + ".def.tcf").exists()) {
                                suffix = ".def.tcf";
                            } else {
                                suffix = ".def.tzf";
                            }
                            JNIHandler.shouldPlayNow = false;
                            JNIHandler.currTime = 0;
                            while (Globals.isPlaying == 0 && !death && shouldAdvance
                                    && !JNIHandler.dataWritten) {
                                try {
                                    Thread.sleep(25);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            Intent new_intent = new Intent(); // silly, but should be done async. I think.
                            new_intent.setAction(getResources().getString(R.string.msrv_rec));
                            new_intent.putExtra(getResources().getString(R.string.msrv_cmd), 17);
                            new_intent.putExtra(getResources().getString(R.string.msrv_infile),
                                    playList.get(songIndex) + suffix);
                            new_intent.putExtra(getResources().getString(R.string.msrv_reset), true);
                            sendBroadcast(new_intent);
                        }
                        while (!death && (((Globals.isPlaying == 0 || JNIHandler.alternativeCheck == 333333)
                                && shouldAdvance))) {
                            try {
                                Thread.sleep(25);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (shouldAdvance && !death) {
                            shouldAdvance = false;
                            new Thread(new Runnable() {
                                public void run() {
                                    if (playList.size() > 1
                                            && (((songIndex + 1 < playList.size() && loopMode == 0))
                                                    || loopMode == 1)) {
                                        next();
                                    } else if (loopMode == 2 || playList.size() == 1) {
                                        play();
                                    } else if (loopMode == 0) {
                                        Globals.hardStop = true;
                                        Intent new_intent = new Intent();
                                        new_intent.setAction(getResources().getString(R.string.ta_rec));
                                        new_intent.putExtra(getResources().getString(R.string.ta_cmd), 5);
                                        new_intent.putExtra(getResources().getString(R.string.ta_pause), false);
                                        sendBroadcast(new_intent);
                                    }
                                }
                            }).start();

                        }

                    }
                }).start();
            }
        }
    }
}