Skip to content

Commit

Permalink
Bug sdcard
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcanterafonseca committed Mar 25, 2014
1 parent 13b00e2 commit 7766fd2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
5 changes: 2 additions & 3 deletions apps/communications/contacts/elements/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ <h1 id='import-settings-title'></h1>
Memory card
<p><span></span><time></time></p>
</button>
<p class="error-message" data-l10n-id="noMemoryCardMsg"></p>
<p class="error-message"></p>
</li>
<li id="import-gmail-option" class="importService" data-source="gmail">
<button class="icon icon-gmail" data-l10n-id="importGmail">
Expand All @@ -105,7 +105,7 @@ <h1 id='import-settings-title'></h1>
<button class="icon icon-sd" data-l10n-id="memoryCard">
Memory card
</button>
<p class="error-message" data-l10n-id="noMemoryCardMsgExport"></p>
<p class="error-message"></p>
</li>
<li id="export-bluetooth-option" data-source="bluetooth">
<button class="icon icon-bluetooth" data-l10n-id="bluetooth">
Expand All @@ -118,4 +118,3 @@ <h1 id='import-settings-title'></h1>
</section>
</template>
</element>

24 changes: 23 additions & 1 deletion apps/communications/contacts/js/utilities/sdcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ if (!utils.sdcard) {
NOT_INITIALIZED: 0,
NOT_AVAILABLE: 1,
AVAILABLE: 2,
SHARED: 3,
observers: {}
};

SdCard.status = SdCard.NOT_INITIALIZED;
SdCard.deviceStorage = navigator.getDeviceStorage('sdcard');

SdCard.updateStorageState = function sd_updateStorageState(state) {
SdCard._toStatus = function toStatus(state) {
switch (state) {
case 'available':
SdCard.status = SdCard.AVAILABLE;
Expand All @@ -22,11 +23,19 @@ if (!utils.sdcard) {
// could have inconsistencies if we allow changing the sdcard
// content meanwhile exporting/importing
case 'shared':
SdCard.status = SdCard.SHARED;
break;
case 'unavailable':
case 'deleted':
SdCard.status = SdCard.NOT_AVAILABLE;
break;
}
};


SdCard.updateStorageState = function sd_updateStorageState(state) {
SdCard._toStatus(state);

Object.keys(this.observers).forEach(function onObserver(name) {
if (typeof(SdCard.observers[name]) === 'function') {
SdCard.observers[name].call(null, state);
Expand Down Expand Up @@ -55,6 +64,19 @@ if (!utils.sdcard) {
return SdCard.status === SdCard.AVAILABLE;
};

SdCard.getStatus = function(cb) {
var req = SdCard.deviceStorage.available();

req.onsuccess = function() {
SdCard._toStatus(req.result);
cb(SdCard.status);
};
req.onerror = function() {
console.error('Error while determining SD Status', req.error.name);
cb(SdCard.status);
};
};

/**
* Retrieves files that adjust to the kind specified in the
* parameters from the SD card inserted in the device.
Expand Down
41 changes: 23 additions & 18 deletions apps/communications/contacts/js/views/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ contacts.Settings = (function() {
newOrderByLastName = null,
ORDER_KEY = 'order.lastname',
PENDING_LOGOUT_KEY = 'pendingLogout',
umsSettingsKey = 'ums.enabled',
bulkDeleteButton;

// Initialise the settings screen (components, listeners ...)
Expand All @@ -48,11 +47,6 @@ contacts.Settings = (function() {
utils.listeners.add({
'#settings-close': hideSettings
});
if (navigator.mozSettings) {
navigator.mozSettings.addObserver(umsSettingsKey, function(evt) {
enableStorageOptions(!evt.settingValue, 'sdUMSEnabled');
});
}

// Subscribe to events related to change state in the sd card
utils.sdcard.subscribeToChanges('check_sdcard', function(value) {
Expand Down Expand Up @@ -372,21 +366,30 @@ contacts.Settings = (function() {
* @param {String} alternativeError Provide an alternative message if sd is
* not enabled despite that the card is present.
*/
var enableStorageOptions = function enableStorageOptions(cardState,
alternativeError) {
var enableStorageOptions = function enableStorageOptions(cardState) {
updateOptionStatus(importSDOption, !cardState, true);
updateOptionStatus(exportSDOption, !cardState, true);

var importSDErrorMessage = 'noMemoryCardMsg';
var exportSDErrorMessage = 'noMemoryCardMsgExport';
if (alternativeError) {
importSDErrorMessage = exportSDErrorMessage = alternativeError;
}
console.log('Status: ', utils.sdcard.status, 'cardState: ', cardState);

if (!cardState || utils.sdcard.status === utils.sdcard.SHARED) {
var importSDErrorMessage = 'noMemoryCardMsg';
var exportSDErrorMessage = 'noMemoryCardMsgExport';
if (utils.sdcard.status === utils.sdcard.SHARED) {
importSDErrorMessage = exportSDErrorMessage = 'sdUMSEnabled';
}

console.log(importSDErrorMessage);

importSDOption.querySelector('p.error-message').textContent =
_(importSDErrorMessage);
exportSDOption.querySelector('p').textContent =
_(exportSDErrorMessage);
importSDOption.querySelector('p.error-message').textContent =
_(importSDErrorMessage);
exportSDOption.querySelector('p').textContent =
_(exportSDErrorMessage);
}
else {
exportSDOption.querySelector('p').textContent = '';
importSDOption.querySelector('p.error-message').textContent = '';
}
};

// Callback that will modify the ui depending if we imported or not
Expand Down Expand Up @@ -959,7 +962,9 @@ contacts.Settings = (function() {
getData();
checkOnline();
checkSIMCard();
enableStorageOptions(utils.sdcard.checkStorageCard());
utils.sdcard.getStatus(function() {
enableStorageOptions(utils.sdcard.checkStorageCard());
});
updateTimestamps();
checkNoContacts();
};
Expand Down

0 comments on commit 7766fd2

Please sign in to comment.